Created
February 21, 2015 01:59
-
-
Save unionx/c579266c52ac68c6e02b to your computer and use it in GitHub Desktop.
Try numba
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from numba import jit | |
from numpy import arange | |
import cProfile | |
##@jit | |
def sum2d(arr): | |
M, N = arr.shape | |
result = 0.0 | |
for i in range(M): | |
for j in range(N): | |
result += arr[i,j] | |
return result | |
a = arange(3000*3000).reshape(3000, 3000) | |
cProfile.run('sum2d(a)') | |
""" | |
without @jit | |
$ ./run.py | |
4 function calls in 2.482 seconds | |
Ordered by: standard name | |
ncalls tottime percall cumtime percall filename:lineno(function) | |
1 0.000 0.000 2.482 2.482 <string>:1(<module>) | |
1 2.482 2.482 2.482 2.482 run.py:8(sum2d) | |
1 0.000 0.000 2.482 2.482 {built-in method exec} | |
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} | |
""" | |
""" | |
with jit | |
$ ./run.py | |
76244 function calls (69807 primitive calls) in 0.105 seconds | |
Ordered by: standard name | |
ncalls tottime percall cumtime percall filename:lineno(function) | |
2 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap>:2264(_handle_fromlist) | |
1 0.017 0.017 0.105 0.105 <string>:1(<module>) | |
1 0.000 0.000 0.000 0.000 <string>:12(__new__) | |
1 0.000 0.000 0.000 0.000 __init__.py:41(__init__) | |
2 0.000 0.000 0.000 0.000 __init__.py:47(create_string_buffer) | |
4 0.000 0.000 0.000 0.000 __init__.py:487(cast) | |
...... | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment