Hi,

I was curious, so here is how to use cython with sympy:

get my "cython" branch, e.g. for example by:

git clone git://github.com/certik/sympy.git
cd sympy
git co -b cython origin/cython

then run in isympy:

In [1]: time divisors(10**8)
CPU times: user 10.70 s, sys: 0.00 s, total: 10.70 s
Wall time: 10.71 s
Out[2]:
[1, 2, 4, 5, 8, 10, 16, 20, 25, 32, 40, 50, 64, 80, 100, 125, 128, 160, 200, 2
50, 256, 320, 400, 500, 625, 640, 800, 1000, 1250, 1280, 1600, 2000, 2500, 312
5, 3200, 4000, 5000, 6250, 6400, 8000, 10000, 12500, 15625, 16000, 20000, 2500
0, 31250, 32000, 40000, 50000, 62500, 78125, 80000, 100000, 125000, 156250, 16
0000, 200000, 250000, 312500, 390625, 400000, 500000, 625000, 781250, 800000,
1000000, 1250000, 1562500, 2000000, 2500000, 3125000, 4000000, 5000000, 625000
0, 10000000, 12500000, 20000000, 25000000, 50000000, 100000000]

Then compile it with cython:

$ ./build.py build_ext --inplace
Compiling module sympy.ntheory.divisors ...
running build_ext
building 'sympy.ntheory.divisors' extension
creating build
creating build/temp.linux-x86_64-2.6
creating build/temp.linux-x86_64-2.6/home
creating build/temp.linux-x86_64-2.6/home/ondrej
creating build/temp.linux-x86_64-2.6/home/ondrej/repos
creating build/temp.linux-x86_64-2.6/home/ondrej/repos/sympy
creating build/temp.linux-x86_64-2.6/home/ondrej/repos/sympy/sympy
creating build/temp.linux-x86_64-2.6/home/ondrej/repos/sympy/sympy/ntheory
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall
-Wstrict-prototypes -fPIC -I/usr/include/python2.6 -c
/home/ondrej/repos/sympy/./sympy/ntheory/divisors.c -o
build/temp.linux-x86_64-2.6/home/ondrej/repos/sympy/./sympy/ntheory/divisors.o
gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions
build/temp.linux-x86_64-2.6/home/ondrej/repos/sympy/./sympy/ntheory/divisors.o
-o sympy/ntheory/divisors.so


and try again:

In [1]: time divisors(10**8)
CPU times: user 0.45 s, sys: 0.00 s, total: 0.45 s
Wall time: 0.45 s
Out[2]:
[1, 2, 4, 5, 8, 10, 16, 20, 25, 32, 40, 50, 64, 80, 100, 125, 128, 160, 200, 2
50, 256, 320, 400, 500, 625, 640, 800, 1000, 1250, 1280, 1600, 2000, 2500, 312
5, 3200, 4000, 5000, 6250, 6400, 8000, 10000, 12500, 15625, 16000, 20000, 2500
0, 31250, 32000, 40000, 50000, 62500, 78125, 80000, 100000, 125000, 156250, 16
0000, 200000, 250000, 312500, 390625, 400000, 500000, 625000, 781250, 800000,
1000000, 1250000, 1562500, 2000000, 2500000, 3125000, 4000000, 5000000, 625000
0, 10000000, 12500000, 20000000, 25000000, 50000000, 100000000]

and you can see, it got 23x faster!

Note that this function uses a very inefficient algorithm, but it's a
perfect candidate for cythonizing (uses just one for loop with
integers) and its a real code in sympy, not something artificial, so
that's why I chose it. Note that just compiling it with cython speeds
it about 1.5x, and adding this .pxd file next to the .py file speeds
it another 15x:

-------
$ cat divisors.pxd
import cython

@cython.locals(i=cython.int)
cpdef divisors(int n)
----------

So that's just to show that it will work.

Ondrej

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to sympy@googlegroups.com
To unsubscribe from this group, send email to sympy+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sympy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to