Advertisement
here2share

# array_timeit.py

Nov 28th, 2019
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.76 KB | None | 0 0
  1. # array_timeit.py
  2.  
  3. from array import *
  4. import timeit
  5.  
  6. test = {'direct min': 99, 'arrays min': 99, 'direct max': 0, 'arrays max': 0}
  7.  
  8. def f1():
  9.     global t
  10.     t += 1
  11.  
  12. def f2():
  13.     p[0] += 1
  14.  
  15. # f1
  16. t = 0
  17.  
  18. # f2
  19. p = array('i', [0])
  20.  
  21. for z in range(10):
  22.  
  23.     a = timeit.timeit("f1()", setup="from __main__ import f1")
  24.     b = timeit.timeit("f2()", setup="from __main__ import f2")
  25.    
  26.     print 'direct =', a
  27.     print 'arrays =', b
  28.     print
  29.    
  30.     test['direct min'] = min(test['direct min'], a)
  31.     test['arrays min'] = min(test['arrays min'], b)
  32.     test['direct max'] = max(test['direct max'], a)
  33.     test['arrays max'] = max(test['arrays max'], b)
  34.  
  35. zzz = 'direct min/arrays min/direct max/arrays max'.split('/')
  36. for z in zzz:
  37.     print z, test[z]
  38. print
  39. print(t)
  40. print(p[0])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement