iterating over strings seems to be really slow?
Rodrick Brown
rodrick.brown at gmail.com
Wed Aug 27 16:53:45 EDT 2014
*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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20140827/0c49813b/attachment.html>
More information about the Python-list
mailing list