Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用正则表达式从%%timeit提取测量值_Python_Python 3.x_Regex_Timeit - Fatal编程技术网

Python 使用正则表达式从%%timeit提取测量值

Python 使用正则表达式从%%timeit提取测量值,python,python-3.x,regex,timeit,Python,Python 3.x,Regex,Timeit,我使用的是Jupyter笔记本,我有一个代码,我将使用%%timeit-o来测量它的性能,然后我需要提取测量值(时间和错误)并将它们存储在一个单独的变量中,我尝试使用res=.返回完整的字符串 之后,我尝试使用regualr表达式解析它,代码如下: timeitResult = res errorPattern = '±(.*)ms' measurePattern = ':(.*)s' error_search = re.search(errorPattern, timeitResult ,

我使用的是Jupyter笔记本,我有一个代码,我将使用
%%timeit-o
来测量它的性能,然后我需要提取测量值(时间和错误)并将它们存储在一个单独的变量中,我尝试使用
res=.
返回完整的字符串
之后,我尝试使用regualr表达式解析它,代码如下:

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)
这只是
错误
,我甚至无法单独获取
度量值
的值


非常感谢您的帮助。

您无需解析字符串即可获得
TimeitResult
对象

只需使用标志:

[2]中的
:%%timeit-o
…:x=10
...:  
...:                                                                                                                                                                                                                                       
每个回路12.3纳秒±2.05纳秒(7次运行的平均值±标准偏差,每个100000000个回路)
出[2]:
在[3]中:
出[3]:
在[4]中:结果=\u
在[5]中:result.best
Out[5]:1.07634271599991E-08
在[7]中:变量(结果)
出[7]:
{'100000000,
“重复”:7,
“最佳”:1.0466937350001899e-08,
“最差”:1.0813086899997871e-08,
“所有运行”:[1.0537269180003932,
1.081308689999787,
1.053099127999758,
1.0665047210000012,
1.04669373500019,
1.0689385099999527,
1.0753222759999517],
“编译时间”:0.0001610000000007775,
"精度":3,,
“时间安排”:[1.0537269180003931e-08,
1.0813086899997871e-08,
1.0530991279997579e-08,
1.066504721000011E-08,
1.0466937350001899e-08,
1.06893850999526E-08,
1.0753222759999516e-08]}
[9]中:result.average
Out[9]:1.0636562825714333e-08
在[10]中:result.stdev
Out[10]:1.1841164471696355e-10

无法执行您的代码,抱歉,有很多名称错误?谢谢你的回答,我知道这一点,但我仍然需要解析,因为我不仅需要得到最佳结果,我还需要得到平均值以及错误值。这都是在
TimeitResult
对象中设置的。请参见编辑后的答案。
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