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