Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2Tools to help benchmarking.
3"""
4from timeit import Timer
5import numpy
8def measure_time(stmt, context, repeat=10, number=50, div_by_number=False):
9 """
10 Measures a statement and returns the results as a dictionary.
12 :param stmt: string
13 :param context: variable to know in a dictionary
14 :param repeat: average over *repeat* experiment
15 :param number: number of executions in one row
16 :param div_by_number: divide by the number of executions
17 :return: dictionary
19 .. runpython::
20 :showcode:
22 from onnxcustom.utils import measure_time
23 from math import cos
25 res = measure_time("cos(x)", context=dict(cos=cos, x=5.))
26 print(res)
28 See `Timer.repeat <https://docs.python.org/3/library/
29 timeit.html?timeit.Timer.repeat>`_
30 for a better understanding of parameter *repeat* and *number*.
31 The function returns a duration corresponding to
32 *number* times the execution of the main statement.
33 """
34 tim = Timer(stmt, globals=context)
35 res = numpy.array(tim.repeat(repeat=repeat, number=number))
36 if div_by_number:
37 res /= number
38 mean = numpy.mean(res)
39 dev = numpy.mean(res ** 2)
40 dev = (dev - mean**2) ** 0.5
41 mes = dict(average=mean, deviation=dev, min_exec=numpy.min(res),
42 max_exec=numpy.max(res), repeat=repeat, number=number)
43 return mes