*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
-- https://mail.python.org/mailman/listinfo/python-list