Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.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/qt/6.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 如何计算我的方法运行所需的时间?_Python - Fatal编程技术网

Python 如何计算我的方法运行所需的时间?

Python 如何计算我的方法运行所需的时间?,python,Python,我有一个有多种方法的程序。我想测量调用每个方法时运行它们所需的时间 比如说 def func1: blah def func2: blah def main: call func1 and func2 and measure their times 有没有一种简单的方法可以做到这一点。如果您是从一个单独的文件运行代码,perf\u counter是一种方法。比如说, from time import perf_counter def main(): start =

我有一个有多种方法的程序。我想测量调用每个方法时运行它们所需的时间

比如说

def func1:
  blah

def func2:
  blah


def main:
  call func1 and func2 and measure their times

有没有一种简单的方法可以做到这一点。

如果您是从一个单独的文件运行代码,
perf\u counter
是一种方法。比如说,

from time import perf_counter

def main():
    start = perf_counter()
    # Do stuff.
    print(perf_counter() - start)
如果您在shell中测试代码,有一种更简单的方法:使用
timeit
模块。它有两个主要功能:
timeit
repeat
。前者只是一个计时器,后者返回不同试验的时间列表。
如果您使用的是导入的函数,请确保将
globals()
作为
globals
参数传递

如果您是从一个单独的文件运行代码,
perf_counter
是一种方法。比如说,

from time import perf_counter

def main():
    start = perf_counter()
    # Do stuff.
    print(perf_counter() - start)
如果您在shell中测试代码,有一种更简单的方法:使用
timeit
模块。它有两个主要功能:
timeit
repeat
。前者只是一个计时器,后者返回不同试验的时间列表。
如果您使用的是导入的函数,请确保将
globals()
作为
globals
参数传递

这是我为自己编写的一个代码计时设置,我使用的是python 2.7

#!python2

import timeit

runs = 100
totalTime = 0.0; average = 0.0
testTimes = []

for i in range(runs):
    startTimer = timeit.default_timer()

    # >>>>> code to be tested goes here <<<<<

    endTimer = timeit.default_timer()

    timeInterval = endTimer - startTimer
    testTimes.append(timeInterval)
    totalTime += timeInterval

    # running below statement causes each run longer to complete
    # print '\n', '%1.4f' % timeInterval + ' seconds'

print
print '   Total time:', '%1.4f' % totalTime + ' seconds'
print 'Shortest time:', '%1.4f' % min(testTimes) + ' seconds'
print ' Longest time:', '%1.4f' % max(testTimes) + ' seconds'
print ' Average time:', '%1.4f' % (totalTime / runs) + ' seconds'
#!蟒蛇2
导入时间信息
运行次数=100
总时间=0.0;平均值=0.0
测试时间=[]
对于范围内的i(运行):
startTimer=timeit.default\u timer()

#>>>>>>要测试的代码放在这里这是我为自己编写的代码计时设置,我使用python 2.7

#!python2

import timeit

runs = 100
totalTime = 0.0; average = 0.0
testTimes = []

for i in range(runs):
    startTimer = timeit.default_timer()

    # >>>>> code to be tested goes here <<<<<

    endTimer = timeit.default_timer()

    timeInterval = endTimer - startTimer
    testTimes.append(timeInterval)
    totalTime += timeInterval

    # running below statement causes each run longer to complete
    # print '\n', '%1.4f' % timeInterval + ' seconds'

print
print '   Total time:', '%1.4f' % totalTime + ' seconds'
print 'Shortest time:', '%1.4f' % min(testTimes) + ' seconds'
print ' Longest time:', '%1.4f' % max(testTimes) + ' seconds'
print ' Average time:', '%1.4f' % (totalTime / runs) + ' seconds'
#!蟒蛇2
导入时间信息
运行次数=100
总时间=0.0;平均值=0.0
测试时间=[]
对于范围内的i(运行):
startTimer=timeit.default\u timer()

#>>>>>>要测试的代码放在这里我发现下面这段可重用代码在测试函数时非常方便

import timeit    

def timed_function(f, *args, **kwargs):
    myname = str(f).split(' ')[1]
    def new_func(*args, **kwargs):
        timer1 = timeit.default_timer()
        result = f(*args, **kwargs)
        timer2 = timeit.default_timer()
        delta = timer2 - timer1
        print('Function {} Time = {:6.3f}ms'.format(myname, delta*1000))
        return result
    return new_func
您可以使用它来修饰任何函数,然后每次运行时它都会打印原始函数的名称和执行时间

大概是这样的:

@timed_function
def func1():
    return sum([0.5 for i in range(10000)])

y = func1()
代码输出:

函数func1时间=0.849ms


[我是从中得到这个想法的。]

我发现下面这段可重用代码在测试函数时非常方便

import timeit    

def timed_function(f, *args, **kwargs):
    myname = str(f).split(' ')[1]
    def new_func(*args, **kwargs):
        timer1 = timeit.default_timer()
        result = f(*args, **kwargs)
        timer2 = timeit.default_timer()
        delta = timer2 - timer1
        print('Function {} Time = {:6.3f}ms'.format(myname, delta*1000))
        return result
    return new_func
您可以使用它来修饰任何函数,然后每次运行时它都会打印原始函数的名称和执行时间

大概是这样的:

@timed_function
def func1():
    return sum([0.5 for i in range(10000)])

y = func1()
代码输出:

函数func1时间=0.849ms


[我是从中得到这个想法的。]

看看这篇文章,看看它是如何做到的。可能重复的,看看这篇文章,看看它是如何做到的。可能重复的