summaryrefslogtreecommitdiff
path: root/benchmarks
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2018-09-22 10:44:51 -0500
committerGitHub <noreply@github.com>2018-09-22 10:44:51 -0500
commit82ea8698c2f06c4c95a7341774faa029ca77e27f (patch)
treecfc191b7a6b1eb70f3c253000b42918f457de37e /benchmarks
parentb72019ce94fceac2585b54fbbd7426aff6b1df78 (diff)
parent62851d46c3eb80546d2fbb64a86f39d85d37b320 (diff)
downloadnumpy-82ea8698c2f06c4c95a7341774faa029ca77e27f.tar.gz
Merge pull request #11959 from hmaarrfk/pad_fill_before_benchmark
MAINT: Explicitely cause pagefaults to happen before starting the benchmarks
Diffstat (limited to 'benchmarks')
-rw-r--r--benchmarks/README.rst8
-rw-r--r--benchmarks/benchmarks/bench_lib.py5
-rw-r--r--benchmarks/benchmarks/bench_ma.py4
-rw-r--r--benchmarks/benchmarks/bench_reduce.py6
-rw-r--r--benchmarks/benchmarks/bench_shape_base.py8
5 files changed, 25 insertions, 6 deletions
diff --git a/benchmarks/README.rst b/benchmarks/README.rst
index f4f0b0de9..b67994ce0 100644
--- a/benchmarks/README.rst
+++ b/benchmarks/README.rst
@@ -60,3 +60,11 @@ Some things to consider:
- Preparing arrays etc. should generally be put in the ``setup`` method rather
than the ``time_`` methods, to avoid counting preparation time together with
the time of the benchmarked operation.
+
+- Be mindful that large arrays created with ``np.empty`` or ``np.zeros`` might
+ not be allocated in physical memory until the memory is accessed. If this is
+ desired behaviour, make sure to comment it in your setup function. If
+ you are benchmarking an algorithm, it is unlikely that a user will be
+ executing said algorithm on a newly created empty/zero array. One can force
+ pagefaults to occur in the setup phase either by calling ``np.ones`` or
+ ``arr.fill(value)`` after creating the array,
diff --git a/benchmarks/benchmarks/bench_lib.py b/benchmarks/benchmarks/bench_lib.py
index 83f26c9d1..e6c91a27c 100644
--- a/benchmarks/benchmarks/bench_lib.py
+++ b/benchmarks/benchmarks/bench_lib.py
@@ -19,7 +19,10 @@ class Pad(Benchmark):
]
def setup(self, shape, pad_width, mode):
- self.array = np.empty(shape)
+ # avoid np.zeros or np.empty's lazy allocation.
+ # np.full causes pagefaults to occur during setup
+ # instead of during the benchmark
+ self.array = np.full(shape, 0)
def time_pad(self, shape, pad_width, mode):
np.pad(self.array, pad_width, mode)
diff --git a/benchmarks/benchmarks/bench_ma.py b/benchmarks/benchmarks/bench_ma.py
index d313f01dc..aff78df0a 100644
--- a/benchmarks/benchmarks/bench_ma.py
+++ b/benchmarks/benchmarks/bench_ma.py
@@ -89,7 +89,9 @@ class Concatenate(Benchmark):
]
def setup(self, mode, n):
- normal = np.zeros((n, n), int)
+ # avoid np.zeros's lazy allocation that cause page faults during benchmark.
+ # np.fill will cause pagefaults to happen during setup.
+ normal = np.full((n, n), 0, int)
unmasked = np.ma.zeros((n, n), int)
masked = np.ma.array(normal, mask=True)
diff --git a/benchmarks/benchmarks/bench_reduce.py b/benchmarks/benchmarks/bench_reduce.py
index 353eb980c..ffc148cd2 100644
--- a/benchmarks/benchmarks/bench_reduce.py
+++ b/benchmarks/benchmarks/bench_reduce.py
@@ -29,8 +29,10 @@ class AddReduceSeparate(Benchmark):
class AnyAll(Benchmark):
def setup(self):
- self.zeros = np.zeros(100000, bool)
- self.ones = np.ones(100000, bool)
+ # avoid np.zeros's lazy allocation that would
+ # cause page faults during benchmark
+ self.zeros = np.full(100000, 0, bool)
+ self.ones = np.full(100000, 0, bool)
def time_all_fast(self):
self.zeros.all()
diff --git a/benchmarks/benchmarks/bench_shape_base.py b/benchmarks/benchmarks/bench_shape_base.py
index b05ea8263..6edad2ea3 100644
--- a/benchmarks/benchmarks/bench_shape_base.py
+++ b/benchmarks/benchmarks/bench_shape_base.py
@@ -23,7 +23,9 @@ class Block(Benchmark):
self.four_1d = np.ones(6 * n)
self.five_0d = np.ones(1 * n)
self.six_1d = np.ones(5 * n)
- self.zero_2d = np.zeros((2 * n, 6 * n))
+ # avoid np.zeros's lazy allocation that might cause
+ # page faults during benchmark
+ self.zero_2d = np.full((2 * n, 6 * n), 0)
self.one = np.ones(3 * n)
self.two = 2 * np.ones((3, 3 * n))
@@ -31,7 +33,9 @@ class Block(Benchmark):
self.four = 4 * np.ones(3 * n)
self.five = 5 * np.ones(1 * n)
self.six = 6 * np.ones(5 * n)
- self.zero = np.zeros((2 * n, 6 * n))
+ # avoid np.zeros's lazy allocation that might cause
+ # page faults during benchmark
+ self.zero = np.full((2 * n, 6 * n), 0)
def time_block_simple_row_wise(self, n):
np.block([self.a_2d, self.b_2d])