指定时间段的时间

时间:2022-11-18 01:26:59

I know I can use timeit like this:

我知道我可以像这样使用timeit:

timeit.timeit("f(x)", "from __main__ import f", number=100000)

And this will repeat f() 100000 times.

这将重复f()100000次。

However, I'd like to do something like this:

但是,我想做这样的事情:

timeit.timeit("f()", "from __main__ import f", duration=5000)

Which would repeat f() as much as necessary until it hits 5 seconds.

哪个会在必要时重复f()直到达到5秒。

Is there something like this in timeit or would I have to craft a while-loop myself?

在timeit中有这样的东西,还是我必须自己制作一个while循环?

2 个解决方案

#1


0  

No, there is no such option. Check out this open issue.

不,没有这样的选择。看看这个未解决的问题。

This is how it is done in timeit.main, which is called when you run something like python -m timeit ... (only including here the relevant part):

这是在timeit.main中完成的,它是在你运行python -m timeit之类的时候调用的...(这里只包括相关部分):

if number == 0:
    # determine number so that 0.2 <= total time < 2.0
    for i in range(1, 10):
        number = 10**i
        try:
            x = t.timeit(number)
        except:
            t.print_exc()
            return 1
        if verbose:
            print "%d loops -> %.*g secs" % (number, precision, x)
        if x >= 0.2:
            break

You can easily modify it to stop after some "total_time".

您可以轻松地将其修改为在“total_time”之后停止。

#2


0  

When run from the command-line,

从命令行运行时

python -mtimeit -s'from script import f, x' 'f(x)'

the timeit script finds the number of iterations needed to call the function for at least 0.2 seconds. Endolith has coalesced that code down to a function:

timeit脚本查找调用函数至少0.2秒所需的迭代次数。 Endolith已将该代码合并为一个函数:

def timeit_auto(stmt="pass", setup="pass", repeat=3, duration=0.2):
    """
    https://*.com/q/19062202/190597 (endolith)
    Imitate default behavior when timeit is run as a script.

    Runs enough loops so that total execution time is greater than 0.2 sec,
    and then repeats that 3 times and keeps the lowest value.

    Returns the number of loops and the time for each loop in microseconds
    """
    t = timeit.Timer(stmt, setup)

    # determine number so that 0.2 <= total time < 2.0
    for i in range(1, 10):
        number = 10 ** i
        x = t.timeit(number)  # seconds
        if x >= duration:
            break
    r = t.repeat(repeat, number)
    best = min(r)
    usec = best * 1e6 / number
    return number, usec

The timeit code in the standard library can be found here.

可以在此处找到标准库中的timeit代码。

#1


0  

No, there is no such option. Check out this open issue.

不,没有这样的选择。看看这个未解决的问题。

This is how it is done in timeit.main, which is called when you run something like python -m timeit ... (only including here the relevant part):

这是在timeit.main中完成的,它是在你运行python -m timeit之类的时候调用的...(这里只包括相关部分):

if number == 0:
    # determine number so that 0.2 <= total time < 2.0
    for i in range(1, 10):
        number = 10**i
        try:
            x = t.timeit(number)
        except:
            t.print_exc()
            return 1
        if verbose:
            print "%d loops -> %.*g secs" % (number, precision, x)
        if x >= 0.2:
            break

You can easily modify it to stop after some "total_time".

您可以轻松地将其修改为在“total_time”之后停止。

#2


0  

When run from the command-line,

从命令行运行时

python -mtimeit -s'from script import f, x' 'f(x)'

the timeit script finds the number of iterations needed to call the function for at least 0.2 seconds. Endolith has coalesced that code down to a function:

timeit脚本查找调用函数至少0.2秒所需的迭代次数。 Endolith已将该代码合并为一个函数:

def timeit_auto(stmt="pass", setup="pass", repeat=3, duration=0.2):
    """
    https://*.com/q/19062202/190597 (endolith)
    Imitate default behavior when timeit is run as a script.

    Runs enough loops so that total execution time is greater than 0.2 sec,
    and then repeats that 3 times and keeps the lowest value.

    Returns the number of loops and the time for each loop in microseconds
    """
    t = timeit.Timer(stmt, setup)

    # determine number so that 0.2 <= total time < 2.0
    for i in range(1, 10):
        number = 10 ** i
        x = t.timeit(number)  # seconds
        if x >= duration:
            break
    r = t.repeat(repeat, number)
    best = min(r)
    usec = best * 1e6 / number
    return number, usec

The timeit code in the standard library can be found here.

可以在此处找到标准库中的timeit代码。