我需要测量具有以下结构的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 test.py
程序运行后,它将为您提供以下输出:
实际 0M0.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)