python - 为什么 Python 的 time.time() 会返回错误的结果?

标签 python windows time windows-8.1 timing

我不是在问 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/

相关文章:

python - 如何在python yaml文件中输出空白值

python - 在 Tornado 中捕获 PIL 异常

windows - 当我从 Windows 快捷方式运行程序时,为什么 wxPerl 不绘制我的 wxStaticBitmap?

c++ - 我可以使用哪种 C++ 软件 IDE 来面向 Windows、Linux 和 OSX?

c++ - 文件系统信息——如何查询?

如果小时数小于 10,JavaScript 会在小时数之前显示零

特定时区的 C++ time_t 到 std::string

c++ - 稳定的时钟在系统范围内稳定吗?

python - 算法:用枕头创建六边形

python - Python中基于标识符的文件并行解析