如何测量Python程序的执行时间(功能结构)?

如何测量Python程序的执行时间(功能结构)?,python,function,time,Python,Function,Time,我需要测量具有以下结构的Python程序的执行时间: import numpy import pandas def func1(): code def func2(): code if __name__ == '__main__': func1() func2() 如果我想使用time.time,我应该将它们放在代码中的什么位置?我想得到整个程序的执行时间 备选案文1: import time start = time.time() import n

我需要测量具有以下结构的Python程序的执行时间:

import numpy
import pandas

def func1():
    code

def func2():
    code

if __name__ == '__main__':

    func1()
    func2()
如果我想使用time.time,我应该将它们放在代码中的什么位置?我想得到整个程序的执行时间

备选案文1:

import time
start = time.time() 

import numpy
import pandas

def func1():
    code

def func2():
    code


if __name__ == '__main__':

    func1()
    func2()


end = time.time()
print("The execution time is", end - start)
备选案文2:

import numpy
import pandas

def func1():
    code

def func2():
    code


if __name__ == '__main__':
    import time
    start = time.time() 

    func1()
    func2()

    end = time.time()
    print("The execution time is", end - start)

在linux中:您可以使用time命令运行这个文件test.py

时间蟒蛇3测试.py

程序运行后,将提供以下输出:

实际0.074s 用户0m0.004s 系统0m0.000s

将告诉您获得整个程序的三次之间的差异:

import time
t1 = time.time() 

import numpy
import pandas

def func1():
    code

def func2():
    code

if __name__ == '__main__':

    func1()
    func2()

t2 = time.time()
print("The execution time is", t2 - t1)

它和OP的帖子有什么不同?