我如何通过使用Numpy与原始Python计算在20000点下计算0≤y≤10的sin(x)之间的执行时间差异? [重复]

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

'''

import timeit
import math
x = np.linspace(0,10,20000)
y = np.cos(x)
%timeit y + 1

(0<=x<=10).all()
for i in range(20000):
    y = math.cos(x)
    %timeit y + 1

'''我在获取原始python代码时遇到错误。我也无法计算出差异。

python time execution
1个回答
0
投票

如何使用time.time()? (我编辑了代码)

import numpy as np
import time
import math

# Using Numpy
x = np.linspace(0, 10, 20000)
start_time = time.time()
y = np.sin(x)
end_time = time.time()
print(end_time - start_time)

# using raw
start_time = time.time()
y = []
x = [0, 10]
for i in range(20000 - 2):
    x.insert(1, i * 10 / 20000)

for element in x:
    y.append(math.cos(element))


end_time = time.time()
print(end_time - start_time)
© www.soinside.com 2019 - 2024. All rights reserved.