Extracting the measured values from %%timeit using regular expression

Viewed 210

I am using Jupyter notebook, and I have a code which I will measure its performance using %%timeit -o after that I need to extract the measured values (time and error) and store each one of them in a separate variable, I tried to use res = _ which will return the full string <TimeitResult : 248 ms ± 27.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)> after that I tried to parse it using regualr expression, with the following code:

timeitResult = res

errorPattern = '±(.*)ms'
measurePattern = ':(.*)s'

error_search = re.search(errorPattern, timeitResult , re.IGNORECASE)
if error_search:
    error= error_search.group(1)

measure_search = re.search(measurePattern , timeitResult , re.IGNORECASE)
if measure_search :
    measure = measure_search .group(1)

print(float(error), float(measure))

but the final result will be: (27.7, 27.7) which is only the error, and I am not even able to fetch the value of measure alone.

Any help would be really appreciated.

1 Answers

You don't have to parse the string to get the TimeitResult object

Just use the -o flag:

In [2]: %%timeit -o  
   ...: x = 10 
   ...:  
   ...:                                                                                                                                                                                                                                       
12.3 ns ± 2.05 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)
Out[2]: <TimeitResult : 12.3 ns ± 2.05 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)>

In [3]: _                                                                                                                                                                                                                                     
Out[3]: <TimeitResult : 12.3 ns ± 2.05 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)>

In [4]: result = _                                                                                                                                                                                                                            

In [5]: result.best                                                                                                                                                                                                                           
Out[5]: 1.0763427159999991e-08

In [7]: vars(result)                                                                                                                                                                                  
Out[7]: 
{'loops': 100000000,
 'repeat': 7,
 'best': 1.0466937350001899e-08,
 'worst': 1.0813086899997871e-08,
 'all_runs': [1.0537269180003932,
  1.081308689999787,
  1.053099127999758,
  1.0665047210000012,
  1.04669373500019,
  1.0689385099999527,
  1.0753222759999517],
 'compile_time': 0.00016100000000007775,
 '_precision': 3,
 'timings': [1.0537269180003931e-08,
  1.0813086899997871e-08,
  1.0530991279997579e-08,
  1.0665047210000011e-08,
  1.0466937350001899e-08,
  1.0689385099999526e-08,
  1.0753222759999516e-08]}

In [9]: result.average                                                                                                                                                                                
Out[9]: 1.0636562825714333e-08

In [10]: result.stdev                                                                                                                                                                                 
Out[10]: 1.1841164471696355e-10
Related