通过使用HERE获得的加速给我留下了深刻的印象numba
今天我在 SO 中发现了一个希望加快代码速度的人提出的问题。所以我想让我们看看我们能用numba
.
这是代码:
from numba import autojit
from time import time
LIMIT = pow(10,6)
def primes(limit):
# Keep only odd numbers in sieve, mapping from index to number is
# num = 2 * idx + 3
# The square of the number corresponding to idx then corresponds to:
# idx2 = 2*idx*idx + 6*idx + 3
sieve = [True] * (limit // 2)
prime_numbers = set([2])
for j in range(len(sieve)):
if sieve[j]:
new_prime = 2*j + 3
prime_numbers.add(new_prime)
for k in range((2*j+6)*j+3, len(sieve), new_prime):
sieve[k] = False
return list(prime_numbers)
numba_primes = autojit(primes)
start = time()
numba_primes(LIMIT)
end=time()
print("Numba: Time Taken : ",end-start)
start = time()
primes(LIMIT)
end=time()
print("Python: Time Taken : ",end-start)
结果:
('Numba: Time Taken : ', 0.68790602684021)
('Python: Time Taken : ', 0.12417221069335938)
为什么会这样?似乎用numba
这段代码并没有变得更快!