Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #77161
| From | Rodrick Brown <rodrick.brown@gmail.com> |
|---|---|
| Date | 2014-08-27 16:53 -0400 |
| Subject | iterating over strings seems to be really slow? |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.13524.1409172863.18130.python-list@python.org> (permalink) |
[Multipart message — attachments visible in raw view] - view raw
*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
else:
if m.has_key(word):
m[word] += 1
else:
m[word] = 1
word=""
return(m)
def wc2():
m={}
for c in s.split():
if m.has_key(c):
m[c] += 1
else:
m[c] = 1
return(m)
if __name__ == '__main__':
import timeit
print(timeit.timeit("wc1()", setup="from __main__ import wc1"))
print(timeit.timeit("wc2()", setup="from __main__ import wc2"))
⮀python wordcount.py
7.39647197723
3.15220093727
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
iterating over strings seems to be really slow? Rodrick Brown <rodrick.brown@gmail.com> - 2014-08-27 16:53 -0400
csiph-web