Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You probably want to use the fast_callable's call_c function. I've posted some examples at http://sagenb.org/home/pub/4086/, but I repeat them below for convenience:

%cython

from sage.ext.interpreters.wrapper_rdf cimport Wrapper_rdf

def iterate_c(Wrapper_rdf f,float n):
    cdef double *result = [n]
    cdef int i
    for i in range(1000):
        f.call_c(result, result)
    return result[0]

def iterate_py(f, float n):
    cdef int i
    for i in range(1000):
        n=f(n)
    return n

Then

g(x)=sqrt(x)
f=fast_callable(g,vars=[x],domain=float)

Timings:

sage: timeit('iterate_c(f,1e23)')
625 loops, best of 3: 18.3 µs per loop
sage: timeit('iterate_py(f,1e23)')
625 loops, best of 3: 162 µs per loop
sage: timeit('iterate_py(g,1e23)')
5 loops, best of 3: 77.2 ms per loop

You probably want to use the fast_callable's call_c function. I've posted some examples at http://sagenb.org/home/pub/4086/, but I repeat them below for convenience:

%cython

from sage.ext.interpreters.wrapper_rdf cimport Wrapper_rdf
from libc.math cimport sqrt

def benchmark(float n):
    for i in range(1000):
        n = sqrt(n)
    return n

def iterate_c(Wrapper_rdf f,float n):
    cdef double *result = [n]
    cdef int i
    for i in range(1000):
        f.call_c(result, result)
    return result[0]

def iterate_py(f, float n):
    cdef int i
    for i in range(1000):
        n=f(n)
    return n

Then

g(x)=sqrt(x)
f=fast_callable(g,vars=[x],domain=float)

Timings:

sage: timeit('benchmark(1e23)')
625 loops, best of 3: 7.15 µs per loop
sage: timeit('iterate_c(f,1e23)')
625 loops, best of 3: 18.3 µs per loop
sage: timeit('iterate_py(f,1e23)')
625 loops, best of 3: 162 µs per loop
sage: timeit('iterate_py(g,1e23)')
5 loops, best of 3: 77.2 ms per loop

You probably want to use the fast_callable's call_c function. I've posted some examples at http://sagenb.org/home/pub/4086/, but I repeat them below for convenience:

%cython

from sage.ext.interpreters.wrapper_rdf cimport Wrapper_rdf
from libc.math cimport sqrt

def benchmark(float n):
    cdef int i
    for i in range(1000):
        n = sqrt(n)
    return n

def iterate_c(Wrapper_rdf f,float n):
    cdef double *result = [n]
    cdef int i
    for i in range(1000):
        f.call_c(result, result)
    return result[0]

def iterate_py(f, float n):
    cdef int i
    for i in range(1000):
        n=f(n)
    return n

Then

g(x)=sqrt(x)
f=fast_callable(g,vars=[x],domain=float)

Timings:

sage: timeit('benchmark(1e23)')
625 loops, best of 3: 7.15 µs per loop
sage: timeit('iterate_c(f,1e23)')
625 loops, best of 3: 18.3 µs per loop
sage: timeit('iterate_py(f,1e23)')
625 loops, best of 3: 162 µs per loop
sage: timeit('iterate_py(g,1e23)')
5 loops, best of 3: 77.2 ms per loop
click to hide/show revision 4
No.4 Revision

You probably want to use the fast_callable's call_c function. I've posted some examples at http://sagenb.org/home/pub/4086/, http://sagenb.org/home/pub/4086/, but I repeat them below for convenience:

%cython

from sage.ext.interpreters.wrapper_rdf cimport Wrapper_rdf
from libc.math cimport sqrt

def benchmark(float n):
    cdef int i
    for i in range(1000):
        n = sqrt(n)
    return n

def iterate_c(Wrapper_rdf f,float n):
    cdef double *result = [n]
    cdef int i
    for i in range(1000):
        f.call_c(result, result)
    return result[0]

def iterate_py(f, float n):
    cdef int i
    for i in range(1000):
        n=f(n)
    return n

Then

g(x)=sqrt(x)
f=fast_callable(g,vars=[x],domain=float)

Timings:

sage: timeit('benchmark(1e23)')
625 loops, best of 3: 7.15 µs per loop
sage: timeit('iterate_c(f,1e23)')
625 loops, best of 3: 18.3 µs per loop
sage: timeit('iterate_py(f,1e23)')
625 loops, best of 3: 162 µs per loop
sage: timeit('iterate_py(g,1e23)')
5 loops, best of 3: 77.2 ms per loop