|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# @license Apache-2.0 |
| 4 | +# |
| 5 | +# Copyright (c) 2020 The Stdlib Authors. |
| 6 | +# |
| 7 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +# you may not use this file except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | + |
| 19 | +"""Benchmark numpy.sum.""" |
| 20 | + |
| 21 | +from __future__ import print_function |
| 22 | +import timeit |
| 23 | + |
| 24 | +NAME = "sum" |
| 25 | +REPEATS = 3 |
| 26 | +ITERATIONS = 1000000 |
| 27 | +COUNT = [0] # use a list to allow modification within nested scopes |
| 28 | + |
| 29 | + |
| 30 | +def print_version(): |
| 31 | + """Print the TAP version.""" |
| 32 | + print("TAP version 13") |
| 33 | + |
| 34 | + |
| 35 | +def print_summary(total, passing): |
| 36 | + """Print the benchmark summary. |
| 37 | +
|
| 38 | + # Arguments |
| 39 | +
|
| 40 | + * `total`: total number of tests |
| 41 | + * `passing`: number of passing tests |
| 42 | +
|
| 43 | + """ |
| 44 | + print("#") |
| 45 | + print("1.." + str(total)) # TAP plan |
| 46 | + print("# total " + str(total)) |
| 47 | + print("# pass " + str(passing)) |
| 48 | + print("#") |
| 49 | + print("# ok") |
| 50 | + |
| 51 | + |
| 52 | +def print_results(iterations, elapsed): |
| 53 | + """Print benchmark results. |
| 54 | +
|
| 55 | + # Arguments |
| 56 | +
|
| 57 | + * `iterations`: number of iterations |
| 58 | + * `elapsed`: elapsed time (in seconds) |
| 59 | +
|
| 60 | + # Examples |
| 61 | +
|
| 62 | + ``` python |
| 63 | + python> print_results(100000, 0.131009101868) |
| 64 | + ``` |
| 65 | + """ |
| 66 | + rate = iterations / elapsed |
| 67 | + |
| 68 | + print(" ---") |
| 69 | + print(" iterations: " + str(iterations)) |
| 70 | + print(" elapsed: " + str(elapsed)) |
| 71 | + print(" rate: " + str(rate)) |
| 72 | + print(" ...") |
| 73 | + |
| 74 | + |
| 75 | +def benchmark(name, setup, stmt, iterations): |
| 76 | + """Run the benchmark and print benchmark results. |
| 77 | +
|
| 78 | + # Arguments |
| 79 | +
|
| 80 | + * `name`: benchmark name (suffix) |
| 81 | + * `setup`: benchmark setup |
| 82 | + * `stmt`: statement to benchmark |
| 83 | + * `iterations`: number of iterations |
| 84 | +
|
| 85 | + # Examples |
| 86 | +
|
| 87 | + ``` python |
| 88 | + python> benchmark("::random", "from random import random;", "y = random()", 1000000) |
| 89 | + ``` |
| 90 | + """ |
| 91 | + t = timeit.Timer(stmt, setup=setup) |
| 92 | + |
| 93 | + i = 0 |
| 94 | + while i < REPEATS: |
| 95 | + print("# python::numpy::" + NAME + name) |
| 96 | + COUNT[0] += 1 |
| 97 | + elapsed = t.timeit(number=iterations) |
| 98 | + print_results(iterations, elapsed) |
| 99 | + print("ok " + str(COUNT[0]) + " benchmark finished") |
| 100 | + i += 1 |
| 101 | + |
| 102 | + |
| 103 | +def main(): |
| 104 | + """Run the benchmarks.""" |
| 105 | + print_version() |
| 106 | + |
| 107 | + iters = ITERATIONS |
| 108 | + n = 10 |
| 109 | + while n < 1e7: |
| 110 | + name = ":len="+str(n) |
| 111 | + setup = "import numpy as np;" |
| 112 | + setup += "x = np.random.random([1,"+str(n)+"]);" |
| 113 | + stmt = "y = np.sum(x);" |
| 114 | + benchmark(name, setup, stmt, iters) |
| 115 | + n *= 10 |
| 116 | + iters /= 4 |
| 117 | + |
| 118 | + print_summary(COUNT[0], COUNT[0]) |
| 119 | + |
| 120 | + |
| 121 | +if __name__ == "__main__": |
| 122 | + main() |
0 commit comments