python - 使用正则表达式从 %%timeit 中提取测量值

标签 python python-3.x regex timeit

我正在使用 Jupyter 笔记本,并且我有一个代码,我将使用 %%timeit -o 来测量其性能之后我需要提取测量值(时间和误差)并将它们存储在一个单独的变量中,我尝试使用 res = _这将返回完整的字符串 <TimeitResult : 248 ms ± 27.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)>之后我尝试使用正则表达式解析它,代码如下:

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

但最终的结果是: (27.7, 27.7) 这只是 error ,我什至无法获取 measure 的值独自一人。

任何帮助将不胜感激。

最佳答案

您不必解析字符串即可获取 TimeitResult 对象

只需使用 -o标志:

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

关于python - 使用正则表达式从 %%timeit 中提取测量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61714022/

相关文章:

python-3.x - flask url_for http 而不是 https

python-3.x - 如何在Python 3中检查汉字是简体还是繁体?

regex - 当传递正则表达式模式的文件时,Egrep 命令挂起

python - 在功能级别记录

python - 在 python 3 中使用 python 2 架子

python - python中的复数

python - 如何使用正则表达式仅匹配 URL 的域部分?

regex - 偶数个 0 或奇数个 1 的二进制数的最短正则表达式

python - 在 Cython 类中,使用 __init__ 和 __cinit__ 有什么区别?

Python 打印到终端 shell unicode