Created
November 17, 2012 14:45
-
-
Save andreasvc/4096484 to your computer and use it in GitHub Desktop.
Benchmark array creation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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