Optimal base for mpz_set_str()

Sam Rawlins sam.rawlins at gmail.com
Tue Sep 21 21:37:33 CEST 2010

Hi All,

I perused the code for mpz_set_str() and in turn mpn_set_str(), but I don't
think I found an answer to "Is there an optimal base for mpz_set_str(),
ceteris paribus?"

One thing I noticed in mpn_set_str() is: if the base is a power of 2, then
the whole process seems much easier, and doesn't involve
mpn_set_str_compute_powtab() or mpn_dc_set_str() at all. So I assume that
using a base which is a power of 2 is faster than otherwise. Can this be
made even more optimal? Is a base of 2 faster than a base of 4, 8, 16, 32,
or slower?

I am tweaking the gmp Ruby gem, specifically how it converts a Ruby Bignum
to an mpz_t, which right now just converts the Ruby Bignum into a Ruby
String (base 10), then a C *char (base 10), and into mpz_set_str(). I've
benchmarked Ruby's Bignum.to_s method which converts a Ruby Bignum to a Ruby
String, with a base between 2 and 36. Converting to a Ruby String base 16
and 32 seem to be the fastest, so I hope to use that.

Any tips or suggestions?

Sam Rawlins

More information about the gmp-discuss mailing list