Hide keyboard shortcuts

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""" 

2@file 

3@brief Measures speed. 

4""" 

5import sys 

6from timeit import Timer 

7import numpy 

8 

9 

10def measure_time(stmt, context, repeat=10, number=50, div_by_number=True): 

11 """ 

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

13 

14 :param stmt: string 

15 :param context: variable to know in a dictionary 

16 :param repeat: average over *repeat* experiment 

17 :param number: number of executions in one row 

18 :param div_by_number: divide by the number of executions 

19 :return: dictionary 

20 

21 .. runpython:: 

22 :showcode: 

23 

24 from math import cos 

25 import pprint 

26 from td3a_cpp.tools import measure_time 

27 

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

29 pprint.pprint(res) 

30 

31 See `Timer.repeat <https://docs.python.org/3/library/ 

32 timeit.html?timeit.Timer.repeat>`_ 

33 for a better understanding of parameter *repeat* and *number*. 

34 The function returns a duration corresponding to 

35 *number* times the execution of the main statement. 

36 """ 

37 tim = Timer(stmt, globals=context) 

38 res = numpy.array(tim.repeat(repeat=repeat, number=number)) 

39 if div_by_number: 

40 res /= number 

41 mean = numpy.mean(res) 

42 dev = numpy.mean(res ** 2) 

43 dev = (dev - mean**2) ** 0.5 

44 mes = dict(average=mean, deviation=dev, min_exec=numpy.min(res), 

45 max_exec=numpy.max(res), repeat=repeat, number=number) 

46 if 'values' in context: 

47 if hasattr(context['values'], 'shape'): 

48 mes['size'] = context['values'].shape[0] 

49 else: 

50 mes['size'] = len(context['values']) 

51 else: 

52 mes['context_size'] = sys.getsizeof(context) 

53 return mes 

54 

55 

56def measure_time_dim(stmt, contexts, repeat=10, number=50, 

57 div_by_number=True, verbose=0): 

58 """ 

59 Measures a statement multiple time with function :func:`measure_time_dim`. 

60 

61 :param stmt: string 

62 :param contexts: variable to know in a dictionary, 

63 every context must include field 'x_name', 

64 which is copied in the result 

65 :param repeat: average over *repeat* experiment 

66 :param number: number of executions in one row 

67 :param div_by_number: divide by the number of executions 

68 :param verbose: if > 0, use :epkg:`tqdm` to display progress 

69 :return: yield dictionary 

70 

71 .. runpython:: 

72 :showcode: 

73 

74 import pprint 

75 import numpy 

76 from td3a_cpp.tools import measure_time_dim 

77 

78 res = list(measure_time_dim( 

79 "cos(x)", 

80 contexts=[dict(cos=numpy.cos, x=numpy.arange(10), x_name=10), 

81 dict(cos=numpy.cos, x=numpy.arange(100), x_name=100)])) 

82 pprint.pprint(res) 

83 

84 See `Timer.repeat <https://docs.python.org/3/library/ 

85 timeit.html?timeit.Timer.repeat>`_ 

86 for a better understanding of parameter *repeat* and *number*. 

87 The function returns a duration corresponding to 

88 *number* times the execution of the main statement. 

89 """ 

90 if verbose > 0: 

91 from tqdm import tqdm 

92 contexts = tqdm(contexts) 

93 

94 for context in contexts: 

95 if 'x_name' not in context: 

96 raise ValueError("The context must contain field 'x_name', " 

97 "usually the X coordinate to draw the benchmark.") 

98 res = measure_time(stmt, context, repeat=repeat, 

99 number=number, div_by_number=div_by_number) 

100 res['x_name'] = context['x_name'] 

101 yield res