简体   繁体   English

纯python比numpy在元素方面的运算要快吗?

[英]Pure python faster than numpy on element-wise operation?

Can someone explain these results to me ? 有人可以向我解释这些结果吗? The pure Python seems to take less time than the numpy expression for element-wise exponentiation. 纯Python似乎比numpy表达式花费更少的时间进行元素取幂。

In [224]: ar=np.arange(1000)
          %timeit a**4
          100000 loops, best of 3: 5.22 µs per loop
In [225]: ar=range(1000)
          %timeit [ar[i]**4 for i in ar]
          1000 loops, best of 3: 205 µs per loop

Numpy total time= 100000*5.22 = 522000 µs 总计时间= 100000 * 5.22 = 522000 µs

Pure Python total time = 1000*205 = 205000 µs 纯Python总时间= 1000 * 205 = 205000 µs

The pure Python version was timed for less iterations. 纯Python版本的定时迭代次数较少。 That doesn't mean it was faster; 这并不意味着它会更快。 that means timeit stopped running it so it wouldn't take 60 seconds* to get results. 这意味着timeit停止运行它,因此不需要60秒*即可获得结果。 You can see from the per-loop time that the NumPy version was 40 times faster. 从每个循环时间可以看到,NumPy版本的速度快40倍。

*200 microseconds per loop * 100,000 loops * 3 repetitions of the timing procedure *每个循环200微秒* 100,000个循环* 3次重复计时过程

You're comparing the result of 1,000 runs against that of 100,000 runs. 您正在将1,000次运行的结果与100,000次运行的结果进行比较。 The numpy version ran 100,000 times and took 5.22 µs each time. numpy版本运行100,000次,每次耗时5.22 µs。 The Python version only ran 1,000 times and took 205 µs per loop. Python版本仅运行1000次,每个循环花费205 µs。

So while the overall timing might have greater, the individual per-operation time for numpy was ~40x less. 因此,尽管总体时序可能更长一些,但numpy的每次操作时间却减少了约40倍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM