On Wed, Nov 23, 2011 at 2:16 PM, rjf <fate...@gmail.com> wrote:
>
> Maxima compiles code to binary, and has done so, oh for a couple of
> decades.
>
>  Since Maxima is part of Sage, one might hope that William would be
> aware of this feature.

In the spirit of being mutually informative, here's how it's done in Sage

>  Example.
>
> g(x):=block([s:0],for i thru x do s:s+i^2,s);

%python
def g(x):
    s = 0
    for i in range(x):
        s += i**2
    return s

> g(10000);  takes 0.15 seconds.

sage: timeit("g(10000)")
625 loops, best of 3: 1.45 ms per loop

> compile(g);   converts g to lisp and compiles it with an optimizing
> compiler.

%cython
def g(x):
    s = 0
    for i in range(x):
        s += i**2
    return s

> g is now about 5X faster.

sage: timeit("g(10000)")
625 loops, best of 3: 833 µs per loop

g is now about 2X faster (though unless your computer is *way* slower
than mine, there was less room for improvement here).

> if one inserts declarations, e.g.  mode_declare(i,fixnum, x, fixnum,
> s, fixnum),
> the code goes about 10X faster.   I think that one can improve the
> speed

%cython
def g_fast(x):
    cdef long i, s = 0
    for i in range(x):
        s += i**2
    return s

sage: timeit("g(10000)")
625 loops, best of 3: 10.9 µs per loop

Well over a 100-fold increase in speed.

Still, if one is dealing with the few parts of Sage that use Maxima,
it could be useful to know about this compile command.

Note that another selling point of Cython is not just writing new
(fast) code, but interfacing with existing low-level libraries in a
clean way.

- Robert

-- 
To post to this group, send an email to sage-devel@googlegroups.com
To unsubscribe from this group, send an email to 
sage-devel+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org

Reply via email to