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