我跑了一個簡單的速度測試,比較numpy和Python列表的理解,顯然列表理解更快。那是對的嗎?numpy vs list comprehension,哪個更快?
import sys, numpy
from datetime import datetime
def numpysum(n):
a = numpy.arange(n) ** 2
b = numpy.arange(n) ** 3
return a + b
def pythonsum(n):
a = [i ** 2 for i in range(n)]
b = [i ** 3 for i in range(n)]
return [a[i] + b[i] for i in range(n)]
size = 10
start = datetime.now()
c1 = pythonsum(size)
delta1 = datetime.now() - start
start = datetime.now()
c2 = numpysum(size)
delta2 = datetime.now() - start
print c1
print c2
print delta1
print delta2