Path: blob/master/ invest-robot-contest_TinkoffBotTwitch-main/venv/lib/python3.8/site-packages/numpy/ma/bench.py
7757 views
#!/usr/bin/env python312import timeit3import numpy456###############################################################################7# Global variables #8###############################################################################91011# Small arrays12xs = numpy.random.uniform(-1, 1, 6).reshape(2, 3)13ys = numpy.random.uniform(-1, 1, 6).reshape(2, 3)14zs = xs + 1j * ys15m1 = [[True, False, False], [False, False, True]]16m2 = [[True, False, True], [False, False, True]]17nmxs = numpy.ma.array(xs, mask=m1)18nmys = numpy.ma.array(ys, mask=m2)19nmzs = numpy.ma.array(zs, mask=m1)2021# Big arrays22xl = numpy.random.uniform(-1, 1, 100*100).reshape(100, 100)23yl = numpy.random.uniform(-1, 1, 100*100).reshape(100, 100)24zl = xl + 1j * yl25maskx = xl > 0.826masky = yl < -0.827nmxl = numpy.ma.array(xl, mask=maskx)28nmyl = numpy.ma.array(yl, mask=masky)29nmzl = numpy.ma.array(zl, mask=maskx)303132###############################################################################33# Functions #34###############################################################################353637def timer(s, v='', nloop=500, nrep=3):38units = ["s", "ms", "µs", "ns"]39scaling = [1, 1e3, 1e6, 1e9]40print("%s : %-50s : " % (v, s), end=' ')41varnames = ["%ss,nm%ss,%sl,nm%sl" % tuple(x*4) for x in 'xyz']42setup = 'from __main__ import numpy, ma, %s' % ','.join(varnames)43Timer = timeit.Timer(stmt=s, setup=setup)44best = min(Timer.repeat(nrep, nloop)) / nloop45if best > 0.0:46order = min(-int(numpy.floor(numpy.log10(best)) // 3), 3)47else:48order = 349print("%d loops, best of %d: %.*g %s per loop" % (nloop, nrep,503,51best * scaling[order],52units[order]))535455def compare_functions_1v(func, nloop=500,56xs=xs, nmxs=nmxs, xl=xl, nmxl=nmxl):57funcname = func.__name__58print("-"*50)59print(f'{funcname} on small arrays')60module, data = "numpy.ma", "nmxs"61timer("%(module)s.%(funcname)s(%(data)s)" % locals(), v="%11s" % module, nloop=nloop)6263print("%s on large arrays" % funcname)64module, data = "numpy.ma", "nmxl"65timer("%(module)s.%(funcname)s(%(data)s)" % locals(), v="%11s" % module, nloop=nloop)66return6768def compare_methods(methodname, args, vars='x', nloop=500, test=True,69xs=xs, nmxs=nmxs, xl=xl, nmxl=nmxl):70print("-"*50)71print(f'{methodname} on small arrays')72data, ver = f'nm{vars}l', 'numpy.ma'73timer("%(data)s.%(methodname)s(%(args)s)" % locals(), v=ver, nloop=nloop)7475print("%s on large arrays" % methodname)76data, ver = "nm%sl" % vars, 'numpy.ma'77timer("%(data)s.%(methodname)s(%(args)s)" % locals(), v=ver, nloop=nloop)78return7980def compare_functions_2v(func, nloop=500, test=True,81xs=xs, nmxs=nmxs,82ys=ys, nmys=nmys,83xl=xl, nmxl=nmxl,84yl=yl, nmyl=nmyl):85funcname = func.__name__86print("-"*50)87print(f'{funcname} on small arrays')88module, data = "numpy.ma", "nmxs,nmys"89timer("%(module)s.%(funcname)s(%(data)s)" % locals(), v="%11s" % module, nloop=nloop)9091print(f'{funcname} on large arrays')92module, data = "numpy.ma", "nmxl,nmyl"93timer("%(module)s.%(funcname)s(%(data)s)" % locals(), v="%11s" % module, nloop=nloop)94return959697if __name__ == '__main__':98compare_functions_1v(numpy.sin)99compare_functions_1v(numpy.log)100compare_functions_1v(numpy.sqrt)101102compare_functions_2v(numpy.multiply)103compare_functions_2v(numpy.divide)104compare_functions_2v(numpy.power)105106compare_methods('ravel', '', nloop=1000)107compare_methods('conjugate', '', 'z', nloop=1000)108compare_methods('transpose', '', nloop=1000)109compare_methods('compressed', '', nloop=1000)110compare_methods('__getitem__', '0', nloop=1000)111compare_methods('__getitem__', '(0,0)', nloop=1000)112compare_methods('__getitem__', '[0,-1]', nloop=1000)113compare_methods('__setitem__', '0, 17', nloop=1000, test=False)114compare_methods('__setitem__', '(0,0), 17', nloop=1000, test=False)115116print("-"*50)117print("__setitem__ on small arrays")118timer('nmxs.__setitem__((-1,0),numpy.ma.masked)', 'numpy.ma ', nloop=10000)119120print("-"*50)121print("__setitem__ on large arrays")122timer('nmxl.__setitem__((-1,0),numpy.ma.masked)', 'numpy.ma ', nloop=10000)123124print("-"*50)125print("where on small arrays")126timer('numpy.ma.where(nmxs>2,nmxs,nmys)', 'numpy.ma ', nloop=1000)127print("-"*50)128print("where on large arrays")129timer('numpy.ma.where(nmxl>2,nmxl,nmyl)', 'numpy.ma ', nloop=100)130131132