简体   繁体   English

python timeit模块重复功能

[英]python timeit module repeat function

I have four functions named numbers_string1() , numbers_string2() , numbers_string3() , numbers_string4() , and I need to find out the execution time of these functions. 我有四个函数, numbers_string2() numbers_string1()numbers_string2()numbers_string3()numbers_string4() ,并且需要找出这些函数的执行时间。

from numstring import *
import time
def profile_clock():
    l=["numbers_string1","numbers_string2","numbers_string3","num_strings4"]
    for i in l:
        count=1
        for j in range(5):
            m=[]
            count=count*10
            m=timeit.repeat("i(count)",5,count)
            #m=t.repeat(5,count)
            print i,(),"count=",count,"min=",min(m),"atuals=",m

When I execute this, I am getting error as global name i is not defined. 当我执行此操作时,由于未定义全局名称i因此出现错误。

The timeit functions run in their own function space. timeit函数在其自己的函数空间中运行。 It doesn't have access to your scope, which is why it provides for a "setup" parameter that it executes to set up the timing environment. 它无权访问您的示波器,这就是为什么它提供一个“ setup”参数并执行该参数来设置计时环境的原因。 You need to define any functions and variables that you need in a string, which you then pass to timeit.repeat as the setup argument. 您需要在字符串中定义所需的任何函数和变量,然后将其作为setup参数传递给timeit.repeat

You seem to be misunderstanding how a couple of other things work, too. 您似乎也误会了其他几件事。 When you put "i" in a string, it's just the letter "i" . 当您将"i"放在字符串中时,它只是字母"i" It doesn't magically take on the value of the variable i . 它不会神奇地采用变量i的值。 For that matter, neither does "count" . 因此, "count"都没有。 You need to create a string that holds some actual values, not variable names. 您需要创建一个包含一些实际值而不是变量名的字符串。

It looks to me like you want to do something like this: 在我看来,您想要执行以下操作:

setup = 'from numstring import *' # String to execute to set up the timeit scope
funcs = ["numbers_string1", "numbers_string2", "numbers_string3", "num_strings4"]
for func in funcs: # Better variable names are better!
    for count in (1, 10, 100, 1000, 10000): # Clean up this iteration
        statement = '{f}({n})'.format(f=func, n=count) # the call to test
        m = timeit.repeat(statement, setup=setup, repeat=5)

I highly recommend that you read the documentation , since m=timeit.repeat("i(count)",5,count) isn't a legal use of the function in the first place. 我强烈建议您阅读文档 ,因为m=timeit.repeat("i(count)",5,count)首先不是对该函数的合法使用。

Tests with timeit are executed in their own separated scope. 使用timeit进行的测试将在各自独立的范围内执行。 This is primarily done so the timing is not influenced by some side effects in the module that is executing the test. 这样做主要是为了使时间不受执行测试的模块中某些副作用的影响。 As such, neither i , nor your functions numbers_string1/2/3/4 are defined during the test. 因此,在测试过程中都未定义i或函数numbers_string1/2/3/4

You can import them by setting them up though, for example: 您可以通过设置它们来导入它们,例如:

timeit.repeat("numbers_string1(count)", 5, count, setup='from __main__ import numbers_string1')

But this won't fix the issue for you because i is still not defined, and even if it was, it would be a string , and not a function you can execute. 但这无法为您解决问题,因为i仍然没有定义,即使定义了它,它也将是字符串 ,而不是可以执行的函数。 Your intention was probably this: 您的意图可能是这样的:

timeit.repeat('{}({})'.format(i, count), repeat=5, setup='from __main import ' + i)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM