def complex_cos(): def real_cos(x): vml.py_vdCos(x[0], x[5]) def complex_cos(x): vml.py_vzCos(x[1], x[4]) def complex_noimag_cos(x): vml.py_vzCos(x[2], x[4]) def complex_noreal_cos(x): vml.py_vzCos(x[3], x[4]) perfplot.show( setup=lambda n: [ np.zeros(n, dtype=np.float64) + 0.25, np.zeros(n, dtype=np.complex128) + (0.25 + .25j), np.zeros(n, dtype=np.complex128) + 0.25, np.zeros(n, dtype=np.complex128) + 0.25j, np.zeros(n, dtype=np.complex128) + 0.25j, np.zeros(n, dtype=np.float64) + 0.25 ], n_range=list(range(1000, 10000, 1000)), kernels=[ real_cos, complex_cos, complex_noimag_cos, complex_noreal_cos ], logx=True, logy=True, title="real vs. complex cos", xlabel='len(vec)', equality_check=None, )
def test_speed(N=2): numpy.random.seed(1) osa = colorio.OsaUcs() cielab = colorio.CIELAB() # cam16 = colorio.CAM16(0.69, 20, L_A=64 / numpy.pi / 5) ciecam02 = colorio.CIECAM02(0.69, 20, L_A=64 / numpy.pi / 5) # This close probably means that another figure hasn't been properly closed. import matplotlib.pyplot as plt plt.close() perfplot.show( # Don't use numpy.random.rand(3, n) to avoid the CIECAM breakdown setup=lambda n: numpy.outer(numpy.random.rand(3), numpy.ones(n)) * 10, equality_check=None, kernels=[ osa.to_xyz100, cielab.to_xyz100, # cam16.to_xyz100, lambda Jsh: ciecam02.to_xyz100(Jsh, "Jsh"), numpy.cbrt, ], labels=["OSA-UCS", "CIELAB", "CIECAM02", "cbrt"], n_range=[2 ** n for n in range(N)], logx=True, logy=True, # relative_to=3 )
def main(): argp = argparse.ArgumentParser() argp.add_argument('-t', '--test', action='store_true', help='run unit test') argp.add_argument('-v', '--verify', action='store_true', help='verify equality of methods') args = argp.parse_args() kernels = [ vec, list_comp, apply, iterrows, itertuples, cython_iter, iloc_iter ] if args.test: print(doctest.testmod()) elif args.verify: df = make_dataframe(100) v = kernels[0](df) for k in kernels[1:]: assert (v == k(df)).all(), f"{k.__name__} doesn't match" else: perfplot.show(setup=lambda n: make_dataframe(n), kernels=kernels, labels=[str(k.__name__) for k in kernels], n_range=[2**k for k in range(16)], xlabel='N', logx=True, logy=True, equality_check=False)
def performance_comparison_from(): import perfplot def setup(n): out = numpy.empty((3, n)) rgb = numpy.random.rand(3) for k in range(3): out[k] = rgb[k] return out Y_b = 20 L_A = 64 / numpy.pi / 5 c = 0.69 # average cam16 = colorio.CAM16(c, Y_b, L_A) cam16_legacy = CAM16Legacy(c, Y_b, L_A) perfplot.show( setup=setup, kernels=[cam16.from_xyz100, cam16_legacy.from_xyz100], labels=["new", "legacy"], n_range=1000 * numpy.arange(6), equality_check=False, ) return
def noerror_impact(): def acos_with_errorhandling(x): vml.py_vmlSetMode(vml.PY_VML_ERRMODE_DEFAULT) vml.py_vdAcos(x[1], x[1]) def acos_without_error_errorhandling(x): vml.py_vmlSetMode(vml.PY_VML_ERRMODE_ERRNO) vml.py_vdAcos(x[1], x[1]) def acos_without_error(x): vml.py_vmlSetMode(vml.PY_VML_ERRMODE_DEFAULT) vml.py_vdAcos(x[0], x[0]) perfplot.show( setup=lambda n: [ np.zeros(int(n), dtype=np.float64) + 0.25, np.zeros(int(n), dtype=np.float64) + 2.0 ], n_range=[1e4, 1e5, 1e6, 1e7, 1e8], kernels=[ acos_without_error, acos_with_errorhandling, acos_without_error_errorhandling ], logx=True, logy=True, title="acos with errors", xlabel='len(vec)', equality_check=None, )
def test0(): perfplot.show( setup=np.random.rand, kernels=kernels, labels=["c_"], n_range=r, xlabel="len(a)", )
def test_flops(): perfplot.show( setup=np.random.rand, kernels=kernels, labels=["c_"], n_range=r, xlabel="len(a)", flops=lambda n: n, )
def test_no_labels(): def mytest(a): return numpy.c_[a, a] kernels = [mytest] r = [2**k for k in range(4)] perfplot.show( setup=numpy.random.rand, kernels=kernels, n_range=r, xlabel='len(a)' ) return
def test1(): perfplot.show( setup=np.random.rand, kernels=kernels, labels=["c_"], n_range=r, xlabel="len(a)", logx=True, logy=False, )
def test_return_tuples(): def setup(n): return np.ones(n), np.full(n + 2, 3.1) def times(a, b): return 2 * a, 3 * b def times_reversed(a, b): return a * 2, b * 3 perfplot.show(setup=setup, kernels=[times, times_reversed], n_range=[2**k for k in range(3)])
def test(): kernels = [lambda a: numpy.c_[a, a]] r = [2**k for k in range(4)] perfplot.show(setup=numpy.random.rand, kernels=kernels, labels=['c_'], n_range=r, xlabel='len(a)') perfplot.show(setup=numpy.random.rand, kernels=kernels, labels=['c_'], n_range=r, xlabel='len(a)', logx=True, logy=False) perfplot.show(setup=numpy.random.rand, kernels=kernels, labels=['c_'], n_range=r, xlabel='len(a)', logx=False, logy=True) perfplot.show(setup=numpy.random.rand, kernels=kernels, labels=['c_'], n_range=r, xlabel='len(a)', logx=True, logy=True) return
def test3(): def setup(n): assert isinstance(n, int) return np.random.rand(n) perfplot.show( setup=setup, kernels=kernels, labels=["c_"], n_range=r, xlabel="len(a)", logx=True, logy=True, )
def plot(title, kernels, labels, n_vals): """Plot timings as a function of the parameter n. Args: title (str): plot title kernels (Sequence[Callable[[Any], Any]]): parametrized benchmarks to time labels (Sequence[str]): names of the kernels n_vals (Sequence[Any]): values the benchmark parameter n takes """ import perfplot perfplot.show( setup=lambda n: n, kernels=kernels, labels=labels, n_range=n_vals, xlabel="n", title=title, )
def service_function_costs(): perfplot.show( setup=lambda n: n, n_range=[1e3, 1e4, 1e5], kernels=[ vml.onoff_multithreading, vml.onoff_multithreading_local, vml.onoff_mode, vml.onoff_mkl_dynamic, ], labels=[ "(2*set+1*get) thread number", "2*set local thread number", "2*set_mode", "(2*set+1*get) mkl_dynamic" ], logx=True, logy=True, title="service functions", xlabel='number of times', equality_check=None, )
def perfplot(): """Convenience entry point to perf-plot different function samples""" import perfplot functions = [] labels = [] for name, func in globals().items(): if name.startswith("_bench"): name = name[1:] functions.append(func) labels.append(name) perfplot.show( setup=lambda n: n, # or simply setup=numpy.random.rand kernels=functions, labels=labels, n_range=[2 ** k for k in range(15)], xlabel="len(a)", equality_check=None, target_time_per_measurement=5.0, time_unit="us", # set to one of ("auto", "s", "ms", "us", or "ns") to force plot units )
def sincos(): def sincos(x): vml.py_vdSinCos(x[0], x[1], x[2]) def sin_and_cos(x): vml.py_vdCos(x[0], x[1]) vml.py_vdSin(x[0], x[2]) perfplot.show( setup=lambda n: [ np.zeros(n, dtype=np.float64) + 0.25, np.zeros(n, dtype=np.float64) + 0.25, np.zeros(n, dtype=np.float64) + 0.25 ], n_range=list(range(1000, 10000, 1000)), kernels=[sincos, sin_and_cos], logx=False, logy=False, title="sincos vs sin+cos", xlabel='len(vec)', equality_check=None, )
def expimag_sincos(): def expimag(x): vml.py_vzExp(x[0], x[1]) def sincos(x): vml.py_vdSinCos(x[2], x[3], x[4]) perfplot.show( setup=lambda n: [ np.zeros(n, dtype=np.complex128) + 0.25j, np.zeros(n, dtype=np.complex128) + 0.25, np.zeros(n, dtype=np.float64) + 0.25, np.zeros(n, dtype=np.float64) + 0.25, np.zeros(n, dtype=np.float64) + 0.25 ], n_range=list(range(1000, 10000, 1000)), kernels=[expimag, sincos], logx=False, logy=False, title="sincos vs expimag", xlabel='len(vec)', equality_check=None, )
def test_speed(n=3): path_pts = [[0, 0], [0, 1], [1, 1], [1, 0]] path0 = path.Path(path_pts) path1 = pypathlib.ClosedPath(path_pts) def _mpl_path(pts): return path0.contains_points(pts) def _pypathlib_contains_points(pts): return path1.contains_points(pts) numpy.random.seed(0) perfplot.show( setup=lambda n: numpy.random.rand(n, 2), kernels=[_mpl_path, _pypathlib_contains_points], n_range=[2 ** k for k in range(n)], labels=["matplotlib.path.contains_points", "pypathlib.contains_points"], logx=True, logy=True, xlabel="num points", ) return
def test(): kernels = [lambda a: numpy.c_[a, a]] r = [2**k for k in range(4)] perfplot.show( setup=numpy.random.rand, kernels=kernels, labels=["c_"], n_range=r, xlabel="len(a)", ) perfplot.show( setup=numpy.random.rand, kernels=kernels, labels=["c_"], n_range=r, xlabel="len(a)", logx=True, logy=False, ) out = perfplot.bench( setup=numpy.random.rand, kernels=kernels, labels=["c_"], n_range=r, xlabel="len(a)", logx=False, logy=True, ) print(out) perfplot.show( setup=numpy.random.rand, kernels=kernels, labels=["c_"], n_range=r, xlabel="len(a)", logx=True, logy=True, ) return
def test(): perfplot.show( setup=numpy.random.rand, kernels=kernels, labels=["c_"], n_range=r, xlabel="len(a)", ) perfplot.show( setup=numpy.random.rand, kernels=kernels, labels=["c_"], n_range=r, xlabel="len(a)", logx=True, logy=False, ) out = perfplot.bench( setup=numpy.random.rand, kernels=kernels, labels=["c_"], n_range=r, xlabel="len(a)", logx=False, logy=True, ) print(out) perfplot.show( setup=numpy.random.rand, kernels=kernels, labels=["c_"], n_range=r, xlabel="len(a)", logx=True, logy=True, ) return
def solve_3(in_val): mat, V = in_val return solve(mat, V, is_flat=False, solver=3) def solve_4(in_val): mat, V = in_val return solve(mat, V, is_flat=False, solver=4) def solve_5(in_val): mat, V = in_val return np.linalg.solve(mat, V) perfplot.show( setup=get_les, kernels=[solve_1, solve_2, solve_3, solve_4, solve_5], labels=[ "PTRANS-I", "PTRANS-II", "scipy.linalg.solve_banded", "scipy.sparse.linalg.spsolve", "numpy.linalg.solve", ], n_range=[2 ** k for k in range(2, 13)], xlabel="Size [n]", logy=True, )
import numpy import perfplot perfplot.show( setup=numpy.random.rand, kernels=[ lambda a: numpy.c_[a, a], lambda a: numpy.stack([a, a]).T, lambda a: numpy.vstack([a, a]).T, lambda a: numpy.column_stack([a, a]), lambda a: numpy.concatenate([a[:, None], a[:, None]], axis=1), ], labels=["c_", "stack", "vstack", "column_stack", "concat"], n_range=[2**k for k in range(15)], xlabel="len(a)", )
"""Benchmark czt.iczt function.""" import numpy as np import czt import perfplot def model(t): output = (1.0 * np.sin(2 * np.pi * 1e3 * t) + 0.3 * np.sin(2 * np.pi * 2e3 * t) + 0.1 * np.sin(2 * np.pi * 3e3 * t)) * np.exp(-1e3 * t) return output perfplot.show( setup=lambda n: czt.czt(model(np.linspace(0, 20e-3, n))), kernels=[ lambda a: czt.iczt(a, simple=True), lambda a: czt.iczt(a, simple=False), ], labels=["simple=True", "simple=False"], n_range=[10**k for k in range(1, 8)], xlabel="Input length", # equality_check=np.allclose, equality_check=False, target_time_per_measurement=0.1, )
x, s, t = ds c, cn = get_pair(x) assert c in s assert cn not in s return True def setup_ds(n): x = numpy.random.rand(n) s = set() t = pyavl.AVL() for xi in x: t.add(xi) s.add(xi) return (x, s, t) perfplot.show( setup=setup_ds, kernels=[ avl_query, set_query, ], n_range=[2**k for k in range(20)], labels=[ "AVL tree", "set", ], title='Querying array performance', xlabel='len(x)', )
import numpy as np import perfplot import colorio np.random.seed(1) osa = colorio.cs.OsaUcs() cielab = colorio.cs.CIELAB() # cam16 = colorio.CAM16(0.69, 20, L_A=64 / np.pi / 5) ciecam02 = colorio.cs.CIECAM02(0.69, 20, L_A=64 / np.pi / 5) perfplot.show( # Don't use np.random.rand(3, n) to avoid the CIECAM breakdown setup=lambda n: np.outer(np.random.rand(3) * 10, np.ones(n)), equality_check=None, kernels=[ osa.to_xyz100, cielab.to_xyz100, # cam16.to_xyz100, lambda Jsh: ciecam02.to_xyz100(Jsh, "Jsh"), # np.cbrt, ], labels=["OSA-UCS", "CIELAB", "CIECAM02", "cbrt"], n_range=[2**n for n in range(23)], # relative_to=3 ) # import tikzplotlib as tpl # tpl.save("out.tex")
num_interior_edges = np.sum(mesh0.is_interior_edge) idx = random.sample(range(num_interior_edges), n // 10) print(num_interior_edges, len(idx), len(idx) / num_interior_edges) # # move interior points a little bit such that we have edges to flip # max_step = np.min(mesh.cell_inradius) / 2 # mesh.points = mesh.points + max_step * np.random.rand(*mesh.points.shape) # print(mesh.num_delaunay_violations) return mesh0, mesh1, idx def flip_old(data): mesh0, mesh1, idx = data mesh0.flip_interior_edges_old(idx) def flip_new(data): mesh0, mesh1, idx = data mesh1.flip_interior_edges(idx) perfplot.show( setup=setup, kernels=[flip_old, flip_new], n_range=[2**k for k in range(5, 13)], equality_check=None, # set target time to 0 to avoid more than one repetition target_time_per_measurement=0.0, )
def parallelization_impact_complex(): def acos(x): vml.py_vzAcos(x[0], x[1]) def cos(x): vml.py_vzCos(x[0], x[1]) def sin(x): vml.py_vzSin(x[0], x[1]) def asin(x): vml.py_vzAsin(x[0], x[1]) def exp(x): vml.py_vzExp(x[0], x[1]) def ln(x): vml.py_vzLn(x[0], x[1]) def mul(x): vml.py_vzMul(x[0], x[0], x[1]) def tan(x): vml.py_vzTan(x[0], x[1]) def atan(x): vml.py_vzAtan(x[0], x[1]) perfplot.show( setup=lambda n: [ np.zeros(n, dtype=np.complex128) + 0.25 + 0.25j, np.zeros(n, dtype=np.complex128) + 0.25 + 0.25j ], n_range=list(range(10, 200, 10)), kernels=[tan, atan], logx=False, logy=False, title="tan, atan", xlabel='len(vec)', equality_check=None, ) perfplot.show( setup=lambda n: [ np.zeros(n, dtype=np.complex128) + 0.25 + 0.25j, np.zeros(n, dtype=np.complex128) + 0.25 + 0.25j ], n_range=list(range(100, 3000, 300)), kernels=[exp, ln, mul], logx=False, logy=False, title="exp, ln, mul", xlabel='len(vec)', equality_check=None, ) perfplot.show( setup=lambda n: [ np.zeros(n, dtype=np.complex128) + 0.25 + 0.25j, np.zeros(n, dtype=np.complex128) + 0.25 + 0.25j ], n_range=list(range(10, 200, 10)), kernels=[sin, cos, asin, acos], logx=False, logy=False, title="sin, cos, asin, acos", xlabel='len(vec)', equality_check=None, )
f = h5py.File(tmp+"with_keys_fixed_copy.hdf",'r') for i in indices_to_read: path = f"event/t{i}" table = f[path+'/block0_values'][:] columns = f[path+'/axis0'][:] indices = f[path +'/axis1'][:] df = pd.DataFrame(table,indices,columns) f.close() # %% def random_samples(lenght): return np.random.randint(0,sample_size,lenght) perfplot.show(setup=random_samples, kernels=[read_with_keys_fixed,read_with_single, read_manual_retrival_with_keys],#[read_with_keys_table,read_with_keys_fixed,read_with_single,read_files], n_range=[ 10000, 20000, 80000], equality_check=None, ) # %% read_with_single([0]) # %% f = pd.HDFStore(tmp+"single.hdf",complib='blosc:zstd') f.keys() # %% f.select('table',"index=='10'") # %% sto = f.get_storer('table') # %%
def set_insert(ds): x, s, t = ds r = random.random() s.add(r) return True def setup_ds(n): x = numpy.random.rand(n) s = set() t = pyavl.AVL() for xi in x: t.add(xi) s.add(xi) return (x, s, t) perfplot.show( setup=setup_ds, kernels=[ avl_insert, set_insert, ], n_range=[2**k for k in range(20)], labels=[ "AVL tree", "set", ], title='Inserting in array performance', xlabel='len(x)', )
def parallelization_impact(): def acos(x): vml.py_vdAcos(x[0], x[1]) def cos(x): vml.py_vdCos(x[0], x[1]) def sin(x): vml.py_vdSin(x[0], x[1]) def asin(x): vml.py_vdAsin(x[0], x[1]) def exp(x): vml.py_vdExp(x[0], x[1]) def ln(x): vml.py_vdLn(x[0], x[1]) def mul(x): vml.py_vdMul(x[0], x[1], x) def tan(x): vml.py_vdTan(x[0], x[1]) def atan(x): vml.py_vdAtan(x[0], x[1]) perfplot.show( setup=lambda n: np.zeros(n, dtype=np.float64) + 0.25, n_range=list(range(1000, 10000, 1000)), kernels=[tan, atan], logx=False, logy=False, title="tan vs atan", xlabel='len(vec)', equality_check=None, ) perfplot.show( setup=lambda n: np.zeros(n, dtype=np.float64) + 0.25, n_range=list(range(1000, 10000, 1000)), kernels=[exp, ln, mul], logx=False, logy=False, title="exp, ln, mul", xlabel='len(vec)', equality_check=None, ) perfplot.show( setup=lambda n: np.zeros(n, dtype=np.float64) + 0.25, n_range=list(range(1000, 10000, 1000)), kernels=[ sin, cos, ], logx=False, logy=False, title="sin, cos", xlabel='len(vec)', equality_check=None, ) perfplot.show( setup=lambda n: np.zeros(n, dtype=np.float64) + 0.25, n_range=list(range(1000, 10000, 1000)), kernels=[ asin, acos, ], logx=False, logy=False, title="asin, acos", xlabel='len(vec)', equality_check=None, )