Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ctypes_timeit.py
- from ctypes import *
- import timeit
- test = {'direct min': 99, 'ctypes min': 99, 'direct max': 0, 'ctypes max': 0}
- def f1():
- global t
- t += 1
- def f2():
- p[0] += 1
- t = 0
- n = c_int(0)
- p = pointer(n)
- for z in range(10):
- a = timeit.timeit("f1()", setup="from __main__ import f1")
- b = timeit.timeit("f2()", setup="from __main__ import f2")
- print 'direct =', a
- print 'ctypes =', b
- print
- test['direct min'] = min(test['direct min'], a)
- test['ctypes min'] = min(test['ctypes min'], b)
- test['direct max'] = max(test['direct max'], a)
- test['ctypes max'] = max(test['ctypes max'], b)
- zzz = 'direct min/ctypes min/direct max/ctypes max'.split('/')
- for z in zzz:
- print z, test[z]
- print
- print(t) # 1000000
- print(n.value) # 1000000
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement