Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Benchmark array creation
import time
import numpy as np
cimport numpy as np
from libc.stdlib cimport malloc, free
from cpython.array cimport array, clone
cdef long N = 1000000
cdef double* ptr
cdef array ar, template = array('d')
def mainmv():
cdef array[double] armv, templatemv = array('d')
print 'array w/mv:',
for L in [1,10,100,1000]: #,10000]:
t1 = time.clock()
for i in range(N):
armv = clone(templatemv, L, False)
t2 = time.clock()
print str((t2-t1)/N*1e6),
print 'us'
print 'Elapsed time to make arrays with lengths [1,10,100,1000,10000]'
print 'array:',
for L in [1,10,100,1000,10000]:
t1 = time.clock()
for i in range(N):
ar = clone(template, L, False)
t2 = time.clock()
print str((t2-t1)/N*1e6),
print 'us'
print 'numpy:',
for L in [1,10,100,1000,10000]:
t1 = time.clock()
npa = np.empty((L,), dtype='double')
for i in range(N):
a = np.empty_like(npa)
t2 = time.clock()
print str((t2-t1)/N*1e6),
print 'us'
print 'raw c-allocation:',
for L in [1,10,100,1000,10000]:
t1 = time.clock()
for i in range(N):
ptr = <double*> malloc(sizeof(double) * L)
free(ptr)
t2 = time.clock()
print str((t2-t1)/N*1e6),
print 'us'
mainmv()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment