Advertisement
maurobaraldi

len vs key count frequency

Sep 18th, 2014
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.58 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #
  3. # Efficiency of len against dict w/ key counting frequency
  4. #
  5. # Inspired in this answer http://stackoverflow.com/a/25919282/841339
  6. #
  7.  
  8. from urllib import urlopen
  9. from string import printable
  10. from time import time
  11.  
  12. chars = {i: 0 for i in printable}
  13. chars['\xef'] = 0
  14. chars['\xbb'] = 0
  15. chars['\xbf'] = 0
  16.  
  17. text = urlopen('http://www.gutenberg.org/cache/epub/4280/pg4280.txt').read()
  18.  
  19. dict_s = time()
  20. for i in text:
  21.     chars[i] += 1
  22. sum(chars.values())
  23. dict_e = time()
  24.  
  25. len_s = time()
  26. len(text)
  27. len_e = time()
  28.  
  29. print len_e-len_s, dict_e-dict_s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement