Case Vanhorsen wrote: > On Wed, Oct 29, 2008 at 11:57 PM, Robert Bradshaw >>> addition times >>> long: 3.03700900078 >>> gmpy: 2.6448340416 >>> gmpy3: 4.70184206963 >> >> Probably the most significant thing >> you could do is construct the mpz class directly rather than call mpz >> () (e.g. see the PY_NEW macro at http://www.sagemath.org/hg/sage-main/ >> file/3859ace86968/c_lib/include/stdsage.h for one way to do that). >> Also, type your "base" parameter to be an int in the __init__ method. >> > By changing "result=mpz()" to "result=PY_NEW(mpz)", the running time > for addition improved to ~4.0 seconds.
Have you tried creating a fast path for mpz values by doing a direct type test instead of calling isinstance()? Cython (like Python 2.6+) provides a C macro "object Py_TYPE(object)", that you can use with "is". Just cdef it in your code to use it. Also note that the special case for int objects in your __add__ method may not be as fast as you might think. It will generate some additional type testing code and will do the ">= 0" comparison and the abs() call through the Python API. Read the generated C code to see what I mean. Try converting the value to a C int right after the type test, before doing anything else, and use the abs() function in math.h (although this will stop working in Py3, where int is long, i.e. no longer compatible with a C int). > I actually used the PY_NEW macro from > http://lists.copyleft.no/pipermail/pyrex/2007-November/002994.html. That macro constructs a global empty tuple reference on the fly. Cython already does that for you, so if you want to cheat, use "__pyx_empty_tuple" instead. (That's not really an official feature, but your compiler will notice it if it ever changes...) Stefan _______________________________________________ Cython-dev mailing list [email protected] http://codespeak.net/mailman/listinfo/cython-dev
