summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatti Picus <matti.picus@gmail.com>2022-12-05 21:31:35 -0500
committerGitHub <noreply@github.com>2022-12-05 21:31:35 -0500
commite877ba95bb3e22238353df0d654ef4d425c42f42 (patch)
treed71d4fead240ebd9543efa40acc2fa8d5cba2377
parent65497a4375e66e80339ffc35156a2604221418b0 (diff)
parent9cf2cef43f683196242aba6450267049d956b41a (diff)
downloadnumpy-e877ba95bb3e22238353df0d654ef4d425c42f42.tar.gz
Merge pull request #22731 from HaoZeke/maBenchCleanup
BENCH: Update MaskedArray Benchmarks
-rw-r--r--benchmarks/benchmarks/bench_ma.py146
-rw-r--r--numpy/ma/bench.py130
-rw-r--r--numpy/tests/test_public_api.py1
3 files changed, 146 insertions, 131 deletions
diff --git a/benchmarks/benchmarks/bench_ma.py b/benchmarks/benchmarks/bench_ma.py
index 0247065a7..49ccf92fe 100644
--- a/benchmarks/benchmarks/bench_ma.py
+++ b/benchmarks/benchmarks/bench_ma.py
@@ -119,3 +119,149 @@ class Concatenate(Benchmark):
def time_it(self, mode, n):
np.ma.concatenate(self.args)
+
+
+class MAFunctions1v(Benchmark):
+ param_names = ['mtype', 'func', 'msize']
+ params = [['np', 'np.ma'],
+ ['sin', 'log', 'sqrt'],
+ ['small', 'big']]
+
+ def setup(self, mtype, func, msize):
+ xs = np.random.uniform(-1, 1, 6).reshape(2, 3)
+ m1 = [[True, False, False], [False, False, True]]
+ xl = np.random.uniform(-1, 1, 100*100).reshape(100, 100)
+ maskx = xl > 0.8
+ self.nmxs = np.ma.array(xs, mask=m1)
+ self.nmxl = np.ma.array(xl, mask=maskx)
+
+ def time_functions_1v(self, mtype, func, msize):
+ # fun = {'np.ma.sin': np.ma.sin, 'np.sin': np.sin}[func]
+ fun = eval(f"{mtype}.{func}")
+ if msize == 'small':
+ fun(self.nmxs)
+ elif msize == 'big':
+ fun(self.nmxl)
+
+
+class MAMethod0v(Benchmark):
+ param_names = ['method', 'msize']
+ params = [['ravel', 'transpose', 'compressed', 'conjugate'],
+ ['small', 'big']]
+
+ def setup(self, method, msize):
+ xs = np.random.uniform(-1, 1, 6).reshape(2, 3)
+ m1 = [[True, False, False], [False, False, True]]
+ xl = np.random.uniform(-1, 1, 100*100).reshape(100, 100)
+ maskx = xl > 0.8
+ self.nmxs = np.ma.array(xs, mask=m1)
+ self.nmxl = np.ma.array(xl, mask=maskx)
+
+ def time_methods_0v(self, method, msize):
+ if msize == 'small':
+ mdat = self.nmxs
+ elif msize == 'big':
+ mdat = self.nmxl
+ getattr(mdat, method)()
+
+
+class MAFunctions2v(Benchmark):
+ param_names = ['mtype', 'func', 'msize']
+ params = [['np', 'np.ma'],
+ ['multiply', 'divide', 'power'],
+ ['small', 'big']]
+
+ def setup(self, mtype, func, msize):
+ # Small arrays
+ xs = np.random.uniform(-1, 1, 6).reshape(2, 3)
+ ys = np.random.uniform(-1, 1, 6).reshape(2, 3)
+ m1 = [[True, False, False], [False, False, True]]
+ m2 = [[True, False, True], [False, False, True]]
+ self.nmxs = np.ma.array(xs, mask=m1)
+ self.nmys = np.ma.array(ys, mask=m2)
+ # Big arrays
+ xl = np.random.uniform(-1, 1, 100*100).reshape(100, 100)
+ yl = np.random.uniform(-1, 1, 100*100).reshape(100, 100)
+ maskx = xl > 0.8
+ masky = yl < -0.8
+ self.nmxl = np.ma.array(xl, mask=maskx)
+ self.nmyl = np.ma.array(yl, mask=masky)
+
+ def time_functions_2v(self, mtype, func, msize):
+ fun = eval(f"{mtype}.{func}")
+ if msize == 'small':
+ fun(self.nmxs, self.nmys)
+ elif msize == 'big':
+ fun(self.nmxl, self.nmyl)
+
+
+class MAMethodGetItem(Benchmark):
+ param_names = ['margs', 'msize']
+ params = [[0, (0, 0), [0, -1]],
+ ['small', 'big']]
+
+ def setup(self, margs, msize):
+ xs = np.random.uniform(-1, 1, 6).reshape(2, 3)
+ m1 = [[True, False, False], [False, False, True]]
+ xl = np.random.uniform(-1, 1, 100*100).reshape(100, 100)
+ maskx = xl > 0.8
+ self.nmxs = np.ma.array(xs, mask=m1)
+ self.nmxl = np.ma.array(xl, mask=maskx)
+
+ def time_methods_getitem(self, margs, msize):
+ if msize == 'small':
+ mdat = self.nmxs
+ elif msize == 'big':
+ mdat = self.nmxl
+ getattr(mdat, '__getitem__')(margs)
+
+
+class MAMethodSetItem(Benchmark):
+ param_names = ['margs', 'mset', 'msize']
+ params = [[0, (0, 0), (-1, 0)],
+ [17, np.ma.masked],
+ ['small', 'big']]
+
+ def setup(self, margs, mset, msize):
+ xs = np.random.uniform(-1, 1, 6).reshape(2, 3)
+ m1 = [[True, False, False], [False, False, True]]
+ xl = np.random.uniform(-1, 1, 100*100).reshape(100, 100)
+ maskx = xl > 0.8
+ self.nmxs = np.ma.array(xs, mask=m1)
+ self.nmxl = np.ma.array(xl, mask=maskx)
+
+ def time_methods_setitem(self, margs, mset, msize):
+ if msize == 'small':
+ mdat = self.nmxs
+ elif msize == 'big':
+ mdat = self.nmxl
+ getattr(mdat, '__setitem__')(margs, mset)
+
+
+class Where(Benchmark):
+ param_names = ['mtype', 'msize']
+ params = [['np', 'np.ma'],
+ ['small', 'big']]
+
+ def setup(self, mtype, msize):
+ # Small arrays
+ xs = np.random.uniform(-1, 1, 6).reshape(2, 3)
+ ys = np.random.uniform(-1, 1, 6).reshape(2, 3)
+ m1 = [[True, False, False], [False, False, True]]
+ m2 = [[True, False, True], [False, False, True]]
+ self.nmxs = np.ma.array(xs, mask=m1)
+ self.nmys = np.ma.array(ys, mask=m2)
+ # Big arrays
+ xl = np.random.uniform(-1, 1, 100*100).reshape(100, 100)
+ yl = np.random.uniform(-1, 1, 100*100).reshape(100, 100)
+ maskx = xl > 0.8
+ masky = yl < -0.8
+ self.nmxl = np.ma.array(xl, mask=maskx)
+ self.nmyl = np.ma.array(yl, mask=masky)
+
+ def time_where(self, mtype, msize):
+ fun = eval(f"{mtype}.where")
+ if msize == 'small':
+ fun(self.nmxs > 2, self.nmxs, self.nmys)
+ elif msize == 'big':
+ fun(self.nmxl > 2, self.nmxl, self.nmyl)
diff --git a/numpy/ma/bench.py b/numpy/ma/bench.py
deleted file mode 100644
index 56865683d..000000000
--- a/numpy/ma/bench.py
+++ /dev/null
@@ -1,130 +0,0 @@
-#!/usr/bin/env python3
-
-import timeit
-import numpy
-
-
-###############################################################################
-# Global variables #
-###############################################################################
-
-
-# Small arrays
-xs = numpy.random.uniform(-1, 1, 6).reshape(2, 3)
-ys = numpy.random.uniform(-1, 1, 6).reshape(2, 3)
-zs = xs + 1j * ys
-m1 = [[True, False, False], [False, False, True]]
-m2 = [[True, False, True], [False, False, True]]
-nmxs = numpy.ma.array(xs, mask=m1)
-nmys = numpy.ma.array(ys, mask=m2)
-nmzs = numpy.ma.array(zs, mask=m1)
-
-# Big arrays
-xl = numpy.random.uniform(-1, 1, 100*100).reshape(100, 100)
-yl = numpy.random.uniform(-1, 1, 100*100).reshape(100, 100)
-zl = xl + 1j * yl
-maskx = xl > 0.8
-masky = yl < -0.8
-nmxl = numpy.ma.array(xl, mask=maskx)
-nmyl = numpy.ma.array(yl, mask=masky)
-nmzl = numpy.ma.array(zl, mask=maskx)
-
-
-###############################################################################
-# Functions #
-###############################################################################
-
-
-def timer(s, v='', nloop=500, nrep=3):
- units = ["s", "ms", "µs", "ns"]
- scaling = [1, 1e3, 1e6, 1e9]
- print("%s : %-50s : " % (v, s), end=' ')
- varnames = ["%ss,nm%ss,%sl,nm%sl" % tuple(x*4) for x in 'xyz']
- setup = 'from __main__ import numpy, ma, %s' % ','.join(varnames)
- Timer = timeit.Timer(stmt=s, setup=setup)
- best = min(Timer.repeat(nrep, nloop)) / nloop
- if best > 0.0:
- order = min(-int(numpy.floor(numpy.log10(best)) // 3), 3)
- else:
- order = 3
- print("%d loops, best of %d: %.*g %s per loop" % (nloop, nrep,
- 3,
- best * scaling[order],
- units[order]))
-
-
-def compare_functions_1v(func, nloop=500,
- xs=xs, nmxs=nmxs, xl=xl, nmxl=nmxl):
- funcname = func.__name__
- print("-"*50)
- print(f'{funcname} on small arrays')
- module, data = "numpy.ma", "nmxs"
- timer("%(module)s.%(funcname)s(%(data)s)" % locals(), v="%11s" % module, nloop=nloop)
-
- print("%s on large arrays" % funcname)
- module, data = "numpy.ma", "nmxl"
- timer("%(module)s.%(funcname)s(%(data)s)" % locals(), v="%11s" % module, nloop=nloop)
- return
-
-def compare_methods(methodname, args, vars='x', nloop=500, test=True,
- xs=xs, nmxs=nmxs, xl=xl, nmxl=nmxl):
- print("-"*50)
- print(f'{methodname} on small arrays')
- data, ver = f'nm{vars}l', 'numpy.ma'
- timer("%(data)s.%(methodname)s(%(args)s)" % locals(), v=ver, nloop=nloop)
-
- print("%s on large arrays" % methodname)
- data, ver = "nm%sl" % vars, 'numpy.ma'
- timer("%(data)s.%(methodname)s(%(args)s)" % locals(), v=ver, nloop=nloop)
- return
-
-def compare_functions_2v(func, nloop=500, test=True,
- xs=xs, nmxs=nmxs,
- ys=ys, nmys=nmys,
- xl=xl, nmxl=nmxl,
- yl=yl, nmyl=nmyl):
- funcname = func.__name__
- print("-"*50)
- print(f'{funcname} on small arrays')
- module, data = "numpy.ma", "nmxs,nmys"
- timer("%(module)s.%(funcname)s(%(data)s)" % locals(), v="%11s" % module, nloop=nloop)
-
- print(f'{funcname} on large arrays')
- module, data = "numpy.ma", "nmxl,nmyl"
- timer("%(module)s.%(funcname)s(%(data)s)" % locals(), v="%11s" % module, nloop=nloop)
- return
-
-
-if __name__ == '__main__':
- compare_functions_1v(numpy.sin)
- compare_functions_1v(numpy.log)
- compare_functions_1v(numpy.sqrt)
-
- compare_functions_2v(numpy.multiply)
- compare_functions_2v(numpy.divide)
- compare_functions_2v(numpy.power)
-
- compare_methods('ravel', '', nloop=1000)
- compare_methods('conjugate', '', 'z', nloop=1000)
- compare_methods('transpose', '', nloop=1000)
- compare_methods('compressed', '', nloop=1000)
- compare_methods('__getitem__', '0', nloop=1000)
- compare_methods('__getitem__', '(0,0)', nloop=1000)
- compare_methods('__getitem__', '[0,-1]', nloop=1000)
- compare_methods('__setitem__', '0, 17', nloop=1000, test=False)
- compare_methods('__setitem__', '(0,0), 17', nloop=1000, test=False)
-
- print("-"*50)
- print("__setitem__ on small arrays")
- timer('nmxs.__setitem__((-1,0),numpy.ma.masked)', 'numpy.ma ', nloop=10000)
-
- print("-"*50)
- print("__setitem__ on large arrays")
- timer('nmxl.__setitem__((-1,0),numpy.ma.masked)', 'numpy.ma ', nloop=10000)
-
- print("-"*50)
- print("where on small arrays")
- timer('numpy.ma.where(nmxs>2,nmxs,nmys)', 'numpy.ma ', nloop=1000)
- print("-"*50)
- print("where on large arrays")
- timer('numpy.ma.where(nmxl>2,nmxl,nmyl)', 'numpy.ma ', nloop=100)
diff --git a/numpy/tests/test_public_api.py b/numpy/tests/test_public_api.py
index 8f654f3d9..28ed2f1df 100644
--- a/numpy/tests/test_public_api.py
+++ b/numpy/tests/test_public_api.py
@@ -280,7 +280,6 @@ PRIVATE_BUT_PRESENT_MODULES = ['numpy.' + s for s in [
"lib.utils",
"linalg.lapack_lite",
"linalg.linalg",
- "ma.bench",
"ma.core",
"ma.testutils",
"ma.timer_comparison",