调试TensorFlow代码,免不了测试某一块程序的运行时间。在Python里,一般用time.time()和time.clock()测试时间。关于这两个函数哪个更准确,查到了如下资料:究竟是使用 time.clock() 精度高,还是使用 time.time() 精度更高,要视乎所在的平台来决定。总概来讲,在 Unix 系统中,建议使用 time.time(),在 Windows 系统中,建议使用 time.clock()。
这个结论也可以在 Python 的 timtit 模块中(用于简单测量程序代码执行时间的内建模块)得到论证:
if sys.platform == "win32":
# On Windows, the best timer is time.clock()
default_timer = time.clock
else:
# On most other platforms the best timer is time.time()
default_timer = time.time
使用 timeit 代替 time,这样就可以实现跨平台的精度性
这么说来,当然更好的实现是用Python 的 timtit 模块:
start = timeit.default_timer()
... do something
elapsed = (timeit.default_timer() - start)
最后一个问题就是,这样计算得到的时间单位是多少?
答案是:“秒”