Advertisement
here2share

# ctypes_timeit.py

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