在跑深度学习模型时,常常需要知道一轮花了多少时间,或者某段代码花了多少时间,以便我们对程序整体的运行效率有更直观的认识,在这里列出几个常用的时间显示代码段,以供参考。
import time
print("Start Time: ", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
start_time = time.time()
"""
在这里写你的代码
"""
end_time = time.time()
print("End Time: ", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
seconds = end_time-start_time
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
print ("Spend Time: %02d:%02d:%02d" % (h, m, s))
import timeit
start_time = timeit.default_timer()
"""
在这里写你的代码
"""
end_time = timeit.default_timer()
time_diff = end_time - start_time
results = "Epoch:{}, Time(sec):{:.3f}, Loss_train:{:.3f}, Loss_dev:{:.3f}"
print(results.format(epoch, time_diff, loss_train, loss_dev))