Skip to content

Instantly share code, notes, and snippets.

@skrah
Created August 21, 2018 11:27
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 skrah/f19f627ada5273491b9547829859442d to your computer and use it in GitHub Desktop.
Save skrah/f19f627ada5273491b9547829859442d to your computer and use it in GitHub Desktop.
Benchmark for threaded XND vs. NumPy
import sys, time
import gumath as gm
import gumath.functions as fn
from xnd import xnd
import numpy as np
import gc
#
# The array sizes are for >=16GB of memory. Your system might freeze if
# you have less than that.
#
#
# Set max_threads to another value (default is num_cpus). In some cases,
# setting max_threads > num_cpus yields a small additional speedup:
#
# gm.set_max_threads(64)
#
max_threads = gm.get_max_threads()
print("\n\n========== sin: max_threads=%d =========\n" % max_threads)
a = np.arange(0, 1, 1/200000000.0, dtype="float64")
x = xnd.from_buffer(a)
start = time.time()
z = fn.sin(x)
end = time.time()
print("xnd: ", end-start)
start = time.time()
c = np.sin(a)
end = time.time()
print("np: ", end-start)
assert np.all(z == c)
del a, x, z, c
gc.collect()
print("\n\n========== multiply: max_threads=%d =========\n" % max_threads)
a = np.arange(150000000.0, dtype="float64")
b = np.arange(150000000.0, dtype="float64")
x = xnd.from_buffer(a)
y = xnd.from_buffer(b)
start = time.time()
z = fn.multiply(x, y)
end = time.time()
print("xnd: ", end-start)
start = time.time()
c = np.multiply(a, b)
end = time.time()
print("np: ", end-start)
assert np.all(z == c)
del a, b, x, y, z, c
gc.collect()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment