Skip to content

Instantly share code, notes, and snippets.

@mattjj
Created October 16, 2012 18:37
Show Gist options
  • Save mattjj/3901139 to your computer and use it in GitHub Desktop.
Save mattjj/3901139 to your computer and use it in GitHub Desktop.
hn wes benchmark followup
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
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()])]
)
@mattjj
Copy link
Author

mattjj commented Oct 16, 2012

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

@mattjj
Copy link
Author

mattjj commented Oct 16, 2012

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.

@mattjj
Copy link
Author

mattjj commented Oct 16, 2012

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