python中获取执行时间函数

发布于 2024-12-20 10:00:06 字数 361 浏览 3 评论 0原文

我创建这个工作函数是为了获取另一个函数的时间:

def get_execution_time(function, args, numberOfExecTime=1):
    """Return the execution time of a function in seconds.

    """

    return round(Timer(partial(function, args)).timeit(numberOfExecTime), 5)

顺便说一句,我有一个问题:我无法向要计时的函数提供多个输入(参数)。 我怎样才能做到这一点?部分是正确的工具吗?

我尝试了装饰器,但无法存储进行一些统计所需的时间。

I create this working function for getting time of another function:

def get_execution_time(function, args, numberOfExecTime=1):
    """Return the execution time of a function in seconds.

    """

    return round(Timer(partial(function, args)).timeit(numberOfExecTime), 5)

By the way I have a problem: I can't give multiple input (args) to the function to be timed.
How can I do that? Is partial the right tool?

I tried decorator but I can't store the time which is what I need for doing some statistics.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

说不完的你爱 2024-12-27 10:00:06

如果您可以牺牲 numberOfExecTime 参数具有默认值的能力,您可以这样做:

from timeit import Timer
from functools import partial

def get_execution_time(function, numberOfExecTime, *args, **kwargs):
    """Return the execution time of a function in seconds."""
    return round(Timer(partial(function, *args, **kwargs))
                 .timeit(numberOfExecTime), 5)

def foo(a, b, c = 12):
    print a, b, c

get_execution_time(foo, 1, 3, 4, c = 14)

或者您可以这样做,但仍然具有 numberOfExecTime 的默认值:

from timeit import Timer
from functools import partial

def get_execution_time(function, *args, **kwargs):
    """Return the execution time of a function in seconds."""
    numberOfExecTime = kwargs.pop('numberOfExecTime', 1)
    return round(Timer(partial(function, *args, **kwargs))
                 .timeit(numberOfExecTime), 5)

def foo(a, b, c = 1):
    print a, b, c

get_execution_time(foo, 1, 2, c = 2)
# => 1 2 2

get_execution_time(foo, 4, 5, c = 3, numberOfExecTime = 2)
# => 4 5 3
# => 4 5 3

If you can sacrifice ability to have default value for numberOfExecTime argument you can do it like that:

from timeit import Timer
from functools import partial

def get_execution_time(function, numberOfExecTime, *args, **kwargs):
    """Return the execution time of a function in seconds."""
    return round(Timer(partial(function, *args, **kwargs))
                 .timeit(numberOfExecTime), 5)

def foo(a, b, c = 12):
    print a, b, c

get_execution_time(foo, 1, 3, 4, c = 14)

Or you can do it like that and still have default value for numberOfExecTime:

from timeit import Timer
from functools import partial

def get_execution_time(function, *args, **kwargs):
    """Return the execution time of a function in seconds."""
    numberOfExecTime = kwargs.pop('numberOfExecTime', 1)
    return round(Timer(partial(function, *args, **kwargs))
                 .timeit(numberOfExecTime), 5)

def foo(a, b, c = 1):
    print a, b, c

get_execution_time(foo, 1, 2, c = 2)
# => 1 2 2

get_execution_time(foo, 4, 5, c = 3, numberOfExecTime = 2)
# => 4 5 3
# => 4 5 3
岁月无声 2024-12-27 10:00:06

您可以传递多个参数:args 应该是包含要传递的参数的元组。

现在,您可以传递 *args 而不是 args

(如果您还需要将 kwargs 传递给您的方法,则在 get_execution_time 中添加另一个参数 kwargs 并将 **kwargs 传递给您的 函数)

def get_execution_time(function, args=(), kwargs ={}, numberOfExecTime=1):
    return round(Timer(partial(function, *args, **kwargs)).timeit(numberOfExecTime), 5)

You can pass multiple arguments: args should be a tuple containing the arguments to pass.

Now, you can pass *args instead of args.

(If you need to also pass kwargs to your method, then have in get_execution_time another argument kwargs and pass **kwargs to your function)

def get_execution_time(function, args=(), kwargs ={}, numberOfExecTime=1):
    return round(Timer(partial(function, *args, **kwargs)).timeit(numberOfExecTime), 5)
人间☆小暴躁 2024-12-27 10:00:06

我会使用计时装饰器。

import time

def timeit(f):

    def timed(*args, **kw):

        ts = time.time()
        result = f(*args, **kw)
        te = time.time()

        print 'func:%r args:[%r, %r] took: %2.4f sec' % \
          (f.__name__, args, kw, te-ts)
        return result

    return timed

使用装饰器很容易或者使用注释。

@timeit
def compute_magic(n):
     #function definition
     #....

或者为您想要计时的函数重新设置别名。

compute_magic = timeit(compute_magic)

我的博客文章在这里有更多信息。 http://blog.mattalcock.com/2013/2/24/计时-python-代码/

I would use a timing decorator.

import time

def timeit(f):

    def timed(*args, **kw):

        ts = time.time()
        result = f(*args, **kw)
        te = time.time()

        print 'func:%r args:[%r, %r] took: %2.4f sec' % \
          (f.__name__, args, kw, te-ts)
        return result

    return timed

Using the decorator is easy either use annotations.

@timeit
def compute_magic(n):
     #function definition
     #....

Or re-alias the function you want to time.

compute_magic = timeit(compute_magic)

My blog post here has more information. http://blog.mattalcock.com/2013/2/24/timing-python-code/

平生欢 2024-12-27 10:00:06

如果 args 是参数列表,您可以使用 *args 扩展它:

return round(Timer(partial(function, args)).timeit(numberOfExecTime), 5)

If args is a list of arguments you can expand it with *args:

return round(Timer(partial(function, args)).timeit(numberOfExecTime), 5)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文