Advertisement
cwchen

test on dict, set, and list

Aug 24th, 2014
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.03 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import csv
  4. import timeit
  5. import os
  6.  
  7. a = list()
  8. d = dict()
  9. s = set()
  10. f = open("drug_list_v3.csv", "rb")
  11. reader = csv.reader(f, delimiter=',')
  12. for row in reader:
  13.     a.append(",".join(row))
  14.     d.setdefault(",".join(row), 1)
  15.     s.add(",".join(row))
  16. f.close()
  17. s1 = """\
  18. o = open("output.csv", 'w')
  19. (o.write(e+"\\n") for e in d.iterkeys())
  20. o.close()
  21. os.remove("output.csv")
  22. """
  23. print "dict: ", timeit.timeit(stmt=s1, setup='from __main__ import d, os',
  24.                               number=100000)
  25. s2 = """\
  26. o = open("output.csv", "w")
  27. (o.write(e+"\\n") for e in s)
  28. o.close()
  29. os.remove("output.csv")
  30. """
  31. print "set: ", timeit.timeit(stmt=s2, setup='from __main__ import s, os',
  32.                              number=100000)
  33. s3 = """\
  34. o = open("output.csv", "w")
  35. (o.write(e+"\\n") for e in set(a))
  36. o.close()
  37. os.remove("output.csv")
  38. """
  39. print "list: ", timeit.timeit(stmt=s3, setup='from __main__ import a, os',
  40.                               number=100000)
  41.  
  42. # o2 = open("output2.csv", "w")
  43. # for e in s:
  44. #     o2.write(e+"\n")
  45. # o2.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement