Created
October 16, 2012 18:37
-
-
Save mattjj/3901139 to your computer and use it in GitHub Desktop.
hn wes benchmark followup
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
import numpy as np | |
cimport numpy as np | |
ctypedef np.float64_t float64_t | |
def inner(np.ndarray[float64_t] x, np.ndarray[float64_t] y): | |
cdef Py_ssize_t i, n = len(x) | |
cdef float64_t result = 0 | |
for i in range(n): | |
result += x[i] * y[i] | |
return result |
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
from distutils.core import setup | |
from distutils.extension import Extension | |
from Cython.Distutils import build_ext | |
import numpy | |
setup( | |
cmdclass = {'build_ext': build_ext}, | |
ext_modules = [Extension("inner", ["inner.pyx"],include_dirs=[numpy.get_include()])] | |
) |
here's the timing of inner()
in Julia:
function inner(x, y)
result = 0.0
for i=1:length(x)
result += x[i] * y[i]
end
result
end
tic()
timing = @time for i=1:50 result = inner(x, y); end
timing = toc();
println("inner took ", (timing / 50) * 1000, " ms")
inner took 37.441840171813965 ms
See Wes's blog post for details on this stuff.
Aaaand finally array operations in Julia (via sum(x.*y)
), which is really the better comparison to the numpy example, took 91.3 ms.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On my i7 920, I got these timings:
Julia
julia> x = randn(1,10000000)
julia> y = randn(10000000,1)
julia> @time for i=1:50 result = x * y; end
elapsed time: 0.9744820594787598 seconds
julia> 0.9744820594787598/50*1000
19.489641189575195
Numpy
In [6]: x = randn(10000000)
In [7]: y = randn(10000000)
In [8]: timeit (x*y).sum()
1 loops, best of 3: 41.1 ms per loop
cython
In [6]: from inner import inner
In [7]: x = randn(10000000)
In [8]: y = randn(10000000)
In [9]: timeit inner(x,y)
100 loops, best of 3: 13.8 ms per loop