Skip to content

Instantly share code, notes, and snippets.

@pstoll
Last active November 17, 2015 04:37
Show Gist options
  • Select an option

  • Save pstoll/ceaf0db8d8827dca8734 to your computer and use it in GitHub Desktop.

Select an option

Save pstoll/ceaf0db8d8827dca8734 to your computer and use it in GitHub Desktop.
python test.py
test1arr for 100000000 primes: write directly to array with new generate_n_primes_array 2.82 sec
test1iter for 100000000 primes: write to numpy data using iter interface: 3.65 sec
test1iter2 for 100000000 primes: write to numpy data using iter interface data access: 3.40 sec
test1np for 100000000 primes: use normal vector generation but copy directly to numpy on output: 4.16 sec
test2 for 100000000 primes: current vector generate_n_primes with numpy copy from vector to output list: 21.03 sec
test2a for 100000000 primes: current vector, copy to output list, create numpy array: 36.25 sec
# let the function create the array and return it
# let the function create the array and return it
def test1arr(sz = 10**8):
import numpy_primesieve as npp
a = npp.generate_n_primes_array(sz,1) # will create output array and return it
#print(a[-1])
return a
def test1np(sz = 10**8):
import numpy_primesieve as npp
a = npp.generate_n_primes_numpy(sz, 1) # will create output array and return it
#print(a[-1])
return a
# pass in the output array - hould have same performace as previous test
def test1iter(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_iter(sz,1, a) # store results into array 'a' and return it
#print(a[-1])
return a
# just create list of primes
def test2(sz = 10**8):
import primesieve as ps
a = ps.generate_n_primes(sz,1)
#print(a[-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))
#print(a[-1])
return a
def time_them():
import timeit
num = 2
sz = 10**8
tests = {'test1arr' : "write directly to array with new generate_n_primes_array",
'test1np' : "use normal vector generation but copy directly to numpy on output:",
'test1iter' : "write to numpy data using iter interface:",
'test1iter2' : "write to numpy data using iter interface data access:",
'test2' : "current vector generate_n_primes with numpy copy from vector to output list:",
'test2a' : "current vector, copy to output list, create numpy array:"
}
for test in sorted(tests.keys()):
msg = tests[test]
call = "{}(sz={})".format(test,sz)
setup = "from __main__ import {}".format(test)
tm = timeit.timeit(call, setup=setup, number=num)
print("{} for {} primes: {} {:.2f} sec".format(test,sz, msg, tm))
if __name__ == '__main__':
time_them()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment