pymen

Python performance access to list, tuple and dicts

May 31st, 2013
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.32 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import time
  3. from numpy.random import random
  4. from collections import OrderedDict, defaultdict
  5.  
  6. def howlong(f):
  7.     def tmp(*args, **kwargs):
  8.         t = time.time()
  9.         res = f(*args, **kwargs)
  10.         need_time = time.time()-t
  11.         tmp.__name__ = f.__name__
  12.         #print "%s time: %f" % ((f.__doc__), need_time)
  13.         #print f.__doc__
  14.         print ".",
  15.         return need_time
  16.     return tmp
  17.  
  18. def test(tests):
  19.  
  20.     results = {}
  21.     for test in tests:
  22.         results[test.__name__] = test()
  23.     print "\nRESULTS"
  24.  
  25.     sorted_results = sorted(results.items(), key=lambda x:x[1] )
  26.     best_name, best_result = sorted_results[0]
  27.     for name, result in sorted_results:
  28.         if name == best_name:
  29.             description = "100%"
  30.         else:
  31.             description = "%s%%" % (int(100 + 100 * (result - best_result) / best_result))
  32.         print "%70s - %20s - %10s" % (name, result, description)
  33.     #print results
  34.  
  35. lst = []
  36. dic = {}
  37. odic = OrderedDict()
  38. ddic = defaultdict(int)
  39.  
  40. for i in range(1, 500000):
  41.     val = random() + i
  42.     lst.append(val)
  43.     dic[i] = val
  44.     odic[i] = val
  45.     ddic[i] = val
  46.  
  47.  
  48. tupl = tuple(lst)
  49.  
  50. NEED_INDEX = (1, 15, 190, 299, 410, 810, 1399, 2200, 2400, 2999, 3100, 3300, 3800, 3900, 4110, 4200, 4500, 4899, 4998, 450000)
  51. NUM_TESTS = 100000
  52.  
  53. @howlong
  54. def test_list_access():
  55.     result = []
  56.     for i in range(NUM_TESTS):
  57.         for j in NEED_INDEX:
  58.             result.append(lst[j])
  59.     return  result
  60.  
  61. @howlong
  62. def test_tuple_access():
  63.     result = []
  64.     for i in range(NUM_TESTS):
  65.         for j in NEED_INDEX:
  66.             result.append(tupl[j])
  67.     return  result
  68.  
  69. @howlong
  70. def test_dic_access():
  71.     result = []
  72.     for i in range(NUM_TESTS):
  73.         for j in NEED_INDEX:
  74.             result.append(dic[j])
  75.     return  result
  76.  
  77. @howlong
  78. def test_odic_access():
  79.     result = []
  80.     for i in range(NUM_TESTS):
  81.         for j in NEED_INDEX:
  82.             result.append(dic[j])
  83.     return  result
  84.  
  85. @howlong
  86. def test_ddic_access():
  87.     result = []
  88.     for i in range(NUM_TESTS):
  89.         for j in NEED_INDEX:
  90.             result.append(dic[j])
  91.     return  result
  92.  
  93.  
  94. tests = [
  95.     test_list_access,
  96.     test_tuple_access,
  97.     test_dic_access,
  98.     test_odic_access,
  99.     test_ddic_access,
  100.     ]
  101. test(tests)
Advertisement
Add Comment
Please, Sign In to add comment