diff options
author | Bas van Beek <43369155+BvB93@users.noreply.github.com> | 2020-08-04 06:00:37 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-04 07:00:37 +0300 |
commit | 593ef5fc5a02fbcd6eeb70a59684b3b21c9cc643 (patch) | |
tree | e811d881547f6cab8998b887d9a7dfa4e5965641 /benchmarks | |
parent | 8f6052280c445328c7f7436917c26fc295429173 (diff) | |
download | numpy-593ef5fc5a02fbcd6eeb70a59684b3b21c9cc643.tar.gz |
ENH: Speed up trim_zeros (#16911)
* Added a benchmark for `trim_zeros()`
* Improve the performance of `np.trim_zeros()`
* Increase the variety of the tests
Fall back to the old `np.trim_zeros()` implementation if an exception is encountered.
Emit a `DeprecationWarning` in such case.
* DEP,REL: Added a deprecation release note
Diffstat (limited to 'benchmarks')
-rw-r--r-- | benchmarks/benchmarks/bench_trim_zeros.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/benchmarks/benchmarks/bench_trim_zeros.py b/benchmarks/benchmarks/bench_trim_zeros.py new file mode 100644 index 000000000..4e25a8b02 --- /dev/null +++ b/benchmarks/benchmarks/bench_trim_zeros.py @@ -0,0 +1,27 @@ +from .common import Benchmark + +import numpy as np + +_FLOAT = np.dtype('float64') +_COMPLEX = np.dtype('complex128') +_INT = np.dtype('int64') +_BOOL = np.dtype('bool') + + +class TrimZeros(Benchmark): + param_names = ["dtype", "size"] + params = [ + [_INT, _FLOAT, _COMPLEX, _BOOL], + [3000, 30_000, 300_000] + ] + + def setup(self, dtype, size): + n = size // 3 + self.array = np.hstack([ + np.zeros(n), + np.random.uniform(size=n), + np.zeros(n), + ]).astype(dtype) + + def time_trim_zeros(self, dtype, size): + np.trim_zeros(self.array) |