Coverage for onnxcustom/utils/benchmark.py: 100%

12 statements  

« prev     ^ index     » next       coverage.py v7.0.5, created at 2023-01-17 01:42 +0100

1""" 

2@file 

3@brief Tools to help benchmarking. 

4""" 

5from timeit import Timer 

6import numpy 

7 

8 

9def measure_time(stmt, context=None, repeat=10, number=50, div_by_number=False): 

10 """ 

11 Measures a statement and returns the results as a dictionary. 

12 

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 

19 

20 .. exref:: 

21 :title: Measure the processing time of a function 

22 

23 .. runpython:: 

24 :showcode: 

25 

26 from onnxcustom.utils import measure_time 

27 from math import cos 

28 

29 res = measure_time("cos(x)", context=dict(cos=cos, x=5.)) 

30 print(res) 

31 

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 or {}) 

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