计算和估算程序运行时间

1 当要运行的数据很大时,可以利用下面的程序估算函数的执行时间,该程序只适用于程序执行时间与执行行数呈一次函数的情况.

import pandas as pd
import datetime as dt
import matplotlib.pyplot as plt

every_time = []
line_number = np.arange(1, 9, 1) * 1000
for j in line_number:
    # 读取的是h5文件时
    # user_repay = pd.read_hdf('user_repay_first.h5')
    # user_repay = user_repay.head(j)
    #读取的是csv文件时
    file1 = pd.read_hdf('user_repay_first.csv', nrows = j)
    time1 = dt.datetime.now()
    file2 = function()
    every_time.append((dt.datetime.now() - time1).total_seconds())
    del file1,file2

func = np.polyfit(line_number,every_time, deg=1)
plt.figure(figsize=(10, 5.5))
plt.plot(line_number, np.polyval(func, line_number))
plt.scatter(line_number,every_time)
plt.show()

print('拟合函数的系数是:', func)
prediction_time = (func[0] * file1.shape[0] + func[1])/3600
print('预计用时:', str(int(prediction_time)) +' hour')

2只需导入time包,在程序开头和结尾加上记录时刻的函数,最后相减

import time

start = time.time()
run_function()
end = time.time()
print str(end)

参考:https://blog.csdn.net/laobai1015/article/details/83618971

原文地址:https://www.cnblogs.com/xxswkl/p/11059679.html