Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; '"the': 0.07; '__name__': 0.09; 'here?': 0.09; 'runs': 0.10; 'python': 0.11; 'def': 0.12; "'__main__':": 0.16; 'subject:slow': 0.16; 'memory': 0.22; 'import': 0.22; 'to:name:python-list@python.org': 0.22; 'pass': 0.26; 'function': 0.29; 'skip:p 30': 0.29; 'array': 0.29; "doesn't": 0.30; 'bigger': 0.30; 'waste': 0.30; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; '"': 0.31; 'skip:7 10': 0.31; 'skip:# 10': 0.33; '"the': 0.34; 'received:google.com': 0.35; 'returning': 0.36; 'subject:?': 0.36; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'black': 0.61; '\xc2\xa0\xc2\xa0': 0.74; 'subject:over': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to:content-type; bh=4UzTMMNiEju3ORtEdX3tOKZ2kc76N3ReBhcEO4p1434=; b=RKNl3COdrn8s0edfn5r4eaSb/udKbAJ2BlV163xtPFG75j7rCLiPA8jWmCStdsMYLO oE+JbHiGSAw422y95vIH239V7UclEe9UE6a/r0C5LujpZq+mqcK3qSReM49p3iBdJMkz JdjG5BjHZsqmyz6jy/4IT/dilcBulEyZJAvgNIZr0yf7T/F/T6Xvz9Tow9rbBGF5Gpt8 GqiCJ3h7RCBtb8MZ7y6v2VO6RlKrU38Z/GLFHgGE5cOjeUUvx0kiej6wBEfHo+RBfhju uhv7R8YS2wD29JgWfuA8DN7Fr5H+z85ZxdhhrwE66Tt0KZxbA9U8Hns/49we40/9ra4o H96g== X-Received: by 10.60.62.234 with SMTP id b10mr37007736oes.3.1409172855102; Wed, 27 Aug 2014 13:54:15 -0700 (PDT) MIME-Version: 1.0 From: Rodrick Brown Date: Wed, 27 Aug 2014 16:53:45 -0400 Subject: iterating over strings seems to be really slow? To: "python-list@python.org" Content-Type: multipart/alternative; boundary=047d7b45102c74a13b0501a29f40 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 317 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1409172864 news.xs4all.nl 2905 [2001:888:2000:d::a6]:38979 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:77161 --047d7b45102c74a13b0501a29f40 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable *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 =3D "The black cat jump over the bigger black cat" def wc1(): word=3D"" m=3D{} for c in s: if c !=3D " ": word +=3D c else: if m.has_key(word): m[word] +=3D 1 else: m[word] =3D 1 word=3D"" return(m) def wc2(): m=3D{} for c in s.split(): if m.has_key(c): m[c] +=3D 1 else: m[c] =3D 1 return(m) if __name__ =3D=3D '__main__': import timeit print(timeit.timeit("wc1()", setup=3D"from __main__ import wc1")) print(timeit.timeit("wc2()", setup=3D"from __main__ import wc2")) =E2=AE=80python wordcount.py 7.39647197723 3.15220093727 --047d7b45102c74a13b0501a29f40 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
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
=C2=A0
s =3D "The black cat j= ump over the bigger black cat"
=C2=A0=C2=A0
def wc1():
word=3D""
m= =3D{}
for c in s:
if c !=3D " ":
wor= d +=3D c
else:
if m.has_key(word):
m[word] +=3D 1
else:
m[word] =3D 1
wor= d=3D""
=C2=A0
return(m)
=C2=A0
=C2=A0
def wc2():
m= =3D{}
for c in s.split():
if m.has_key(c):
m[c] +=3D 1
else:
m[c] =3D 1
return(m)
=C2=A0
if __na= me__ =3D=3D '__main__':
import timeit
print(timei= t.timeit("wc1()", setup=3D"from __main__ import wc1= "))
print(timei= t.timeit("wc2()", setup=3D"from __main__ import wc2= "))
=C2=A0
=E2=AE=80python word= count.py
7.39647197723
3.15220093727
--047d7b45102c74a13b0501a29f40--