# gmpy_cffi is slow

Today I released version 0.1 of gmpy_cffi, a PyPy compatible implementation of gmpy. The problem is that my implementation is quite a bit slower.

As you can see, the time taken to initialize an mpz instance is about 10x slower under gmpy_cffi when compared to gmpy.

# Explanation

The gmpy mpz initialisation code uses the CPython internals to access the raw bits storing the value of a python integer (or long). In comparison, gmpy_cffi converts the number to a hex string and then uses mpz_set_str with base 16 to set the GMP mpz value

I’ve tried other methods such as using bit twiddling to break the python int into an array of C ints and then using mpz_import, but it was slower than using hex

# Conclusion

The conversion from python long to gmp mpz is by far the main reason why initializing mpzs in gmpy_cffi is so slow, but there isn’t really an obvious way to improve on this.

Perhaps cffi could provide a fast method for converting python longs to an array of C ints, but I doubt there’s a huge demand for that right now.

We’re within a factor of 10 of evil low level C hackery. For now that will have to do.