Ask Your Question

Revision history [back]

Check out the cython command that is available by default in Sage. It does what you want and I think it happens to be the command that I used to implement the%cython mode in the notebook.

sage: cython?
....
       Given a block of Cython (Sage's variant of Pyrex) code (as a text
       string), this function compiles it using your C compiler, and
       includes it into the global scope of the module that called this
       function.
sage: cython('def f(double n): return n*n')
sage: f(292908234982340982)
8.5795234120470249e+34

Here is a longer example:

sage: cython("""
....: def f(int i):
....:     cdef int j
....:     for j in range(i):
....:         i += j
....:     return i
....: """)
sage: f(100)
5050
sage: timeit('f(100)')
625 loops, best of 3: 465 ns per loop