Skip to content

Instantly share code, notes, and snippets.

@pstoll
Created November 15, 2015 19:19
Show Gist options
  • Save pstoll/ceb08c6b3baad648423c to your computer and use it in GitHub Desktop.
Save pstoll/ceb08c6b3baad648423c to your computer and use it in GitHub Desktop.
Test timing of writing arrays in place vs using vectors for numpy primesieve bindings
pstoll$ python test.py
test1 arrays: 6.956779105006717
test1a arrays: 6.8869501760054845
test2 native vectors: 59.97818154899869
test2a native vectors: 90.65342697499727
# let the function create the array and return it
def test1():
sz = 10**8
import numpy_primesieve as npp
a = npp.generate_n_primes_array(sz,1) # will create output array and return it
return a
# pass in the output array - hould have same performace as previous test
def test1a():
sz = 10**8
import numpy_primesieve as npp
import numpy as np
a = np.zeros([sz],dtype=np.uint64,order='c')
npp.generate_n_primes_array(sz,1, a) # store results into array 'a' and return it
return a
# just create list of primes
def test2():
sz = 10**8
import primesieve as ps
a = ps.generate_n_primes(sz,1)
return a
# create list of primes and use it to create a numpy array
def test2a():
sz = 10**8
import primesieve as ps
import numpy as np
a = np.array(ps.generate_n_primes(sz,1))
return a
def time_them():
import timeit
num = 5
print("test1 arrays:", timeit.timeit("test1()", setup="from __main__ import test1", number=num))
print("test1a arrays:", timeit.timeit("test1a()", setup="from __main__ import test1a", number=num))
print("test2 native vectors:",timeit.timeit("test2()", setup="from __main__ import test2", number=num))
print("test2a native vectors:",timeit.timeit("test2a()", setup="from __main__ import test2a", number=num))
if __name__ == '__main__':
time_them()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment