Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- #
- # Efficiency of len against dict w/ key counting frequency
- #
- # Inspired in this answer http://stackoverflow.com/a/25919282/841339
- #
- from urllib import urlopen
- from string import printable
- from time import time
- chars = {i: 0 for i in printable}
- chars['\xef'] = 0
- chars['\xbb'] = 0
- chars['\xbf'] = 0
- text = urlopen('http://www.gutenberg.org/cache/epub/4280/pg4280.txt').read()
- dict_s = time()
- for i in text:
- chars[i] += 1
- sum(chars.values())
- dict_e = time()
- len_s = time()
- len(text)
- len_e = time()
- print len_e-len_s, dict_e-dict_s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement