Python vs Cython Speed Test

78 views
Skip to first unread message

austin aigbe

unread,
Sep 29, 2016, 4:41:55 AM9/29/16
to cython-users
Hello,

I ran a speed test between Python and Cython. I noticed that the Python module was faster than the Cython module. Why? Did I get something wrong?

# c2d_3.pyx

from libc.stdio cimport printf
from libc.math cimport sqrt, sin

def test(long long n):
cdef long long i
cdef float j
for i in range(n):
if i > 0:
j = (sqrt(2)*(1/sin(i)))


# c2d_2.py:

from math import sqrt, sin

def test(n):
for i in range(n):
if i > 0:
j = sqrt(2)*(1/sin(i))



c2d_3.pyx was compiled using the VC++ Compiler for Python2.7:
==================================================

# setup.py

from setuptools import setup
from setuptools import Extension
from Cython.Build import cythonize

setup(
    ext_modules = cythonize("c2d_3.pyx")
)


c:\Users\eausaig\Documents\c2D\src>python setup.py build_ext --inplace

Compiling c2d_3.pyx because it changed.
[1/1] Cythonizing c2d_3.pyx
running build_ext
building 'c2d_3' extension
C:\Users\eausaig\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -IC:\Python27\include -IC:\Python27\PC /Tcc2d_3.c /Fobuild\temp.win32-2.7\Release\c2d_3.obj
c2d_3.c
c2d_3.c(729) : warning C4244: 'function' : conversion from '__int64' to 'double', possible loss of data
c2d_3.c(734) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
C:\Users\eausaig\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\link.exe /DLL /nologo /INCREMENTAL:NO /LIBPATH:C:\Python27\libs /LIBPATH:C:\Python27\PCbuild /EXPORT:initc2d_3 build\temp.win32-2.7\Release\c2d_3.obj /OUT:build\lib.win32-2.7\c2d_3.pyd /IMPLIB:build\temp.win32-2.7\Release\c2d_3.lib /MANIFESTFILE:build\temp.win32-2.7\Release\c2d_3.pyd.manifest
   Creating library build\temp.win32-2.7\Release\c2d_3.lib and object build\temp.win32-2.7\Release\c2d_3.exp
copying build\lib.win32-2.7\c2d_3.pyd ->



Test result:
==============

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\eausaig>cd Documents\c2D\src

C:\Users\eausaig\Documents\c2D\src>python -m timeit -s "import c2d_3; c2d_3.test(20000)"
10000000 loops, best of 3: 0.0231 usec per loop =====================================> Cython

C:\Users\eausaig\Documents\c2D\src>python -m timeit -s "import c2d_2; c2d_2.test(20000)"
10000000 loops, best of 3: 0.0197 usec per loop =====================================> Python

Marcel Martin

unread,
Sep 29, 2016, 5:19:45 AM9/29/16
to cython...@googlegroups.com
On 2016-09-29 10:41, austin aigbe wrote:
> C:\Users\eausaig\Documents\c2D\src>python -m timeit -s "import c2d_3;
> c2d_3.test(20000)"
> 10000000 loops, best of 3: 0.0231 usec per loop

You haven’t actually given timeit a statement to measure. The -s option
to timeit is just for setup instructions (run once before the
measurement loop). If you leave out the statement, timeit will only
measure how long it takes to run a pass statement. You should use

python -m timeit -s "import c2d_3" "c2d_3.test(20000)"

Marcel

austin aigbe

unread,
Sep 29, 2016, 6:05:49 AM9/29/16
to cython-users
Thanks for point this out. I knew I was doing something wrong.


C:\Users\eausaig\Documents\c2D\src>python -m timeit -s "import c2d_3" "c2d_3.test(20000)"
1000 loops, best of 3: 1.08 msec per loop  ========================================> Cython 10 times faster

C:\Users\eausaig\Documents\c2D\src>python -m timeit -s "import c2d_2" "c2d_2.test(20000)"
100 loops, best of 3: 10 msec per loop ============================================> Python
Reply all
Reply to author
Forward
0 new messages