1 | initial version |
Here are a few minor suggestions for squeezing out a little extra speed in your function.
In the Cython code you wrote above you use the Python abs
and int
function. This induces coercion of your fast C-data types to slow Python-data types. I suggest starting your speedups by using the C-abs
function and <int>
, instead.
%cython
cdef extern from "math.h":
double fabs(double)
cpdef double g2(double x):
x = fabs(x)
x = 4.0*(x/4.0 - <int>(x/4.0))
if x <= 2.0:
return 1.0-x
return -3.0+x
I tried using floor(fabs(x/4.0))
instead of <int>(x/4.0)
but I didn't get any additional speedup. Also, cpdef
improves performance if you call this function from Cython code.
2 | No.2 Revision |
Here are a few minor suggestions for squeezing out a little extra speed in your function.
In the Cython code you wrote above you use the Python abs
and int
function. This induces coercion of your fast C-data types to slow Python-data types. I suggest starting your speedups by using the C-abs
function and <int>
, instead.
%cython
cdef extern from "math.h":
double fabs(double)
cpdef double g2(double x):
x = fabs(x)
x = 4.0*(x/4.0 - <int>(x/4.0))
if x <= 2.0:
return 1.0-x
return -3.0+x
I tried using floor(fabs(x/4.0))
instead of <int>(x/4.0)
but I didn't get any additional speedup. Also, cpdef
improves performance if you call this function from Cython code.code. In particular, the $f(x)$ you define above should be a Cython function as well so you can take full advantage of the Cython speed benefits.