Reputation: 2946
Here is some code that I wrote to test numba
import time
from numba import autojit
@autojit
def triple_count(num):
start = time.time()
count=0.0
r=range(num)
for i in r:
for j in r:
for k in r:
count += i*j*k
runtime=time.time()-start
rate=num/runtime
print "%0.4f seconds" % runtime
print "rate : %0.3f per second" % rate
print "count: %s" % count
The result when @autojit is commented out
2.3796 seconds
rate : 126.073 per second
count: 9.0216784125e+13
The result when using @autojit
0.0000 seconds
rate : 8065969.231 per second
count: 0.0
What is going wrong here?
Upvotes: 1
Views: 300
Reputation: 68682
I would first start out by timing things differently, not inserting timing calls within the method and instead return the result and then time the method call:
from numba import autojit
def triple_count(num):
count=0.0
r=range(num)
for i in r:
for j in r:
for k in r:
count += i*j*k
return count
triple_count_numba = autojit(triple_count)
For num=200
:
In [7]: triple_count(200)
Out[7]:
7880599000000.0
In [8]:triple_count_numba(200)
Out[8]:
7880599000000.0
In [9]: %timeit triple_count(200)
1 loops, best of 3: 747 ms per loop
In [10]: %timeit triple_count_numba(200)
100 loops, best of 3: 6.59 ms per loop
So the numba produces the same answer as the vanilla python and gets it quite a bit faster. With num=500
it's 11.5 s vs 106 ms. I'm using numba 0.14.0. You could use timeit programatically in a standard python script, but I'm using it in an IPython notebook using the %magics for convenience.
Upvotes: 1