Skip to content

Instantly share code, notes, and snippets.

@TimothyFitz
Created August 21, 2012 19:20
Show Gist options
  • Save TimothyFitz/3418485 to your computer and use it in GitHub Desktop.
Save TimothyFitz/3418485 to your computer and use it in GitHub Desktop.
ctypes slow
import ctypes, time, array
count = 64 ** 2 * 24
t0 = time.time()
for x in range(100):
v = [0] * count
t1 = time.time()
print "python list", (t1-t0)*10, 'ms'
t2 = time.time()
for x in range(100):
cv = (ctypes.c_float * count)(*v)
t3 = time.time()
print "ctypes constructor", (t3-t2)*10, 'ms'
t4 = time.time()
cv = (ctypes.c_float * count)()
for x in range(100):
for v in range(count):
cv[v] = 0.0
t5 = time.time()
print "ctypes setitem", (t5-t4)*10, 'ms'
t6 = time.time()
cv = [0] * count
for x in range(100):
for v in range(count):
cv[v] = 0.0
t7 = time.time()
print "list setitem", (t7-t6)*10, 'ms'
t8 = time.time()
for x in range(100):
cv = array.array('f', [0] * count)
t9 = time.time()
print "array constructor", (t9-t8)*10, 'ms'
"""
Results:
(Windows 7)
$ pypy ./gistfile1.py
python list 0.350000858307 ms
ctypes constructor 50.720000267 ms
ctypes setitem 44.5799994469 ms
list setitem 0.95999956131 ms
array constructor 6.07000112534 ms
$ python ./gistfile1.py
python list 0.490019321442 ms
ctypes constructor 22.9613208771 ms
ctypes setitem 19.2010879517 ms
list setitem 15.6108999252 ms
array constructor 5.01028060913 ms
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment