Skip to content

Instantly share code, notes, and snippets.

@andreasvc
Created November 17, 2012 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreasvc/4096484 to your computer and use it in GitHub Desktop.
Save andreasvc/4096484 to your computer and use it in GitHub Desktop.
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