我不是在问 time.time()
和 time.clock()
之间的区别,我是在问为什么返回的结果完全错误.
它说已经过去了 37 秒,而实际上已经过去了大约 5 秒...这是我的操作系统时钟的问题吗?我的 python 安装?我四处搜索,但找不到遇到同样问题的人。
这是我的代码:
t0 = time.time()
weightMatrix.MultiprocessMatrix(sigmaI, sigmaX)
t1 = time.time() - t0
print('Parallel took {} seconds'.format(t1))
t0 = time.time()
weightMatrix.CreateMatrix(sigmaI, sigmaX)
t2 = time.time() - t0
print('Serial took {} seconds'.format(t2))
print('Parallel time was {}% faster than serial time!'.format((t2/t1)*100))
第二个,t2
看起来是准确的,但第一个报告的数字比它应该的大得多。
Edit2:我只是在想,time.time()
据说只测量时钟时间。但是 time.clock()
测量处理器时间。我正在使用 time()
,所以这应该不是问题,但是...我正在尝试比较函数的串行方法和并行方法。如果一个人连续运行多个进程,它实际上会比一个内核测量更多的处理时间?但同样,使用 time()
应该无关紧要。我不知道,因为第二个(串行函数)似乎可以正确测量时间。
最佳答案
根据帮助文档,time.clock():
Help on built-in function clock in module time:
clock(...) clock() -> floating point number
Return the CPU time or real time since the start of the process or since the first call to clock(). This has as much precision as the system records.
所以我不知道你在没有代码的情况下想做什么,但我猜这与 time.clock() 正在做的事情和你想完成的事情有关?
关于python - 为什么 Python 的 time.time() 会返回错误的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31731189/