Since the Cython notebook was created, the module, cython_import
has been updated, extended and renamed to cython_compile
It can be used similar to cython_import
but in addition it can be invoked via a pythonic decorator syntax.
Futhermore an experimental autodeclaration wrapper has been added
In [5]:
p = 32416190071 # large prime
In [6]:
import math
def pycheck(p):
for y in xrange(2, int(math.sqrt(p)) + 1):
if p % y == 0:
return False
return True
%timeit pycheck(p)
Check the module cycheck.py
It contains three functions
pure
variable declarationscdef
variable delcarationsRun the script below and notice the difference
In [7]:
from cython_compile import cython_import
cython_import('cycheck')
import cycheck
%timeit pycheck(p)
%timeit cycheck.cycheck(p)
%timeit cycheck.cycheck_pure(p)
%timeit cycheck.cycheck_cdef(p)
So far the module is similar to the previous cython_import
module, but in the next section the difference will become clear
In [8]:
from cython_compile import cython_compile
@cython_compile
def pycheck(p):
import math
for y in xrange(2, int(math.sqrt(p)) + 1):
if p % y == 0:
return False
return True
%timeit pycheck(p)
In [9]:
import cython
from cython_compile import cython_compile
@cython_compile
@cython.ccall
@cython.locals(y=cython.int, p=cython.ulonglong)
def pycheck_pure(p):
import math
for y in xrange(2, int(math.sqrt(p)) + 1):
if p % y == 0:
return False
return True
%timeit pycheck_pure(p)
In [10]:
from cython_compile import cython_compile
@cython_compile
def pycheck_cdef(p): #cpdef pycheck_cdef(unsigned long long p):
import math
#cdef int y
for y in xrange(2, int(math.sqrt(p)) + 1):
if p % y == 0:
return False
return True
%timeit pycheck_cdef(p)
In [11]:
from cython_compile import cython_compile_autodeclare
@cython_compile_autodeclare
def pycheck(p):
import math
for y in xrange(2, int(math.sqrt(p)) + 1):
if p % y == 0:
return False
return True
%timeit pycheck(17)
In [ ]: