蟒time.clock()经过较大值时仅通过在几秒钟

问题描述 投票:0回答:1

我的代码

start = time.clock();
# Do something here
end = time.clock() - start;
print end;

只有在6秒钟过去了,但这个方案回报说18秒钟过去了。我已经试过了time.time()和timeit模块,但其结果是相同的。这是怎么发生的?

编辑:我使用Python 2.7在Ubuntu 16.04。我执行的代码是一个机器学习算法,数据集是不小。它包括三个最可疑的功能,例如像numpy.linalg.solve(),numpy.tanh()和scipy.linalg.orth()。除了这些,剩下的代码是正常的代码。

EDIT2:是的,问题应该解决。运行下面的代码

import numpy as np
from numpy.linalg import solve
import random
import time

start = time.clock();

M = np.random.rand( 10000, 10000 );
b = np.random.rand( 10000, 1);
ans = solve(M, b);

print time.clock() - start;

如果你的内存不够,只是缩小尺寸。它是约10秒,但输出为107秒。

python timing
1个回答
0
投票

关于你的代码中的几句话:

  • Python并不需要分号,你应该将其删除
  • 您使用time,但你不能将其导入

如果我解决您的代码并运行它,它打印1.8363000000021223e-05

注意在最后的E-05,这是为0.000018363科学记数法..

import time
start = time.clock()
# Do something here
end = time.clock() - start
print(end)

另外请注意,你不应该在Python 3.3或更高版本使用.clock()。您应该改用time.perf_counter或time.process_time。

© www.soinside.com 2019 - 2024. All rights reserved.