import time import math import numpy as np x = [i * 0.001 for i in np.arange(1000000)] start = time.perf_counter() for i, t in enumerate(x): x[i] = math.sin(t) print("math.sin:", time.perf_counter() - start) x = [i * 0.001 for i in np.arange(1000000)] x = np.array(x) start = time.perf_counter() np.sin(x) print("numpy.sin:", time.perf_counter() - start) x1 = np.random.rand(1000000) x2 = np.random.rand(1000000) tic = time.process_time() dot = 0 for i in range(len(x1)): dot += x1[i]*x2[i] toc = time.process_time() print("dot = " + str(dot) + "\n for loop----- Computation time = " + str(1000*(toc - tic)) + "ms") tic = time.process_time() dot = 0 dot = np.dot(x1, x2) toc = time.process_time() print("dot =" + str(dot) + "\n verctor version---- Computation time = " + str(1000*(toc - tic)) + "ms")
python深度学习基于pytorch代码1.6math与numpy对比
最新推荐文章于 2024-06-03 21:01:49 发布