Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #77162

Re: iterating over strings seems to be really slow?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder3.xlned.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python.list@tim.thechases.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'here?': 0.09; 'slow.': 0.09; 'runs': 0.10; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; '-tkc': 0.16; 'collections': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'subject:slow': 0.16; 'surprises': 0.16; 'wrote:': 0.18; 'memory': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'pass': 0.26; 'header :In-Reply-To:1': 0.27; 'function': 0.29; 'array': 0.29; "doesn't": 0.30; 'bigger': 0.30; 'waste': 0.30; 'skip:# 10': 0.33; '"the': 0.34; 'returning': 0.36; 'charset:us-ascii': 0.36; 'subject:?': 0.36; 'black': 0.61; 'to:addr:gmail.com': 0.65; 'results': 0.69; 'received:50.22': 0.84; 'subject:over': 0.84
Date Wed, 27 Aug 2014 16:21:57 -0500
From Tim Chase <python.list@tim.thechases.com>
To Rodrick Brown <rodrick.brown@gmail.com>
Subject Re: iterating over strings seems to be really slow?
In-Reply-To <CABRP1o-X3wadeNh-7sf7XvEFGAus-QLQ=FTEab3hRT+y2EzyWA@mail.gmail.com>
References <CABRP1o-X3wadeNh-7sf7XvEFGAus-QLQ=FTEab3hRT+y2EzyWA@mail.gmail.com>
X-Mailer Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu)
Mime-Version 1.0
Content-Type text/plain; charset=US-ASCII
Content-Transfer-Encoding 7bit
X-AntiAbuse This header was added to track abuse, please include it with any abuse report
X-AntiAbuse Primary Hostname - boston.accountservergroup.com
X-AntiAbuse Original Domain - python.org
X-AntiAbuse Originator/Caller UID/GID - [47 12] / [47 12]
X-AntiAbuse Sender Address Domain - tim.thechases.com
X-Get-Message-Sender-Via boston.accountservergroup.com: authenticated_id: tim@thechases.com
Cc "python-list@python.org" <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.13525.1409174624.18130.python-list@python.org> (permalink)
Lines 56
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1409174624 news.xs4all.nl 2949 [2001:888:2000:d::a6]:42742
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:77162

Show key headers only | View raw


On 2014-08-27 16:53, Rodrick Brown wrote:
> *I'm confused why the former function runs significantly faster when
> wc1() builds the hash on a single pass and doesn't waste memory of
> returning an array of strings? *
> 
> *I would think wc2() to be slower what's going on here? *
> 
> 
> #!/usr/bin/env python
> 
> s = "The black cat jump over the bigger black cat"
> 
> 
> def wc1():
>     word=""
>     m={}
>     for c in s:
>         if c != " ":
>             word += c

String-building a character-at-a-time is slow.  Also, it doesn't
produce the same results as wc2() does.  Check

  if wc1() == wc2():
    print("Success")
  else:
    print("doh!")

> def wc2():
>     m={}
>     for c in s.split():
>         if m.has_key(c):
>             m[c] += 1
>         else:
>             m[c] = 1
>     return(m)

The thing that surprises me is that using collections.Counter() and
collections.defaultdict(int) are also slower than your wc2():

from collections import defaultdict, Counter

def wc3():
    return Counter(s.split())

def wc4():
    m = defaultdict(int)
    for c in s.split():
        m[c] += 1
    return m

-tkc



Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: iterating over strings seems to be really slow? Tim Chase <python.list@tim.thechases.com> - 2014-08-27 16:21 -0500

csiph-web