speed of mpz_odd_p and lazy allocation
marc.glisse at inria.fr
Tue Aug 21 22:33:01 CEST 2012
On Tue, 21 Aug 2012, Marc Glisse wrote:
> I guess I should simulate the gains for std::vector, since those would
> also apply to gmpxx and not just external wrappers.
I made a very quick test. All I did was change the C++ wrapper so that
when I move-construct from a value, instead of calling mpz_init for the
moved-from object, I just set _mp_d to 0 (I should have used _mp_alloc
according to the conversation, but that's just a test). And in the
destructor, I call mpz_clear only if _mp_d!=0. Clearly this modification
is incomplete, it is just to get a feeling for potential gain.
I create a vector<mpz_class> and append a 0 (resize) to it a few million
times. With -std=c++11 and the current code, I get a time of 1. With
-std=c++03, I get 1.5. And with the small patch described above, I get
I am a bit surprised that c++11 helps so much even with an old gmpxx.h
without move constructors/assignment. It seems to be because libstdc++
copies the 0s in c++03 and constructs them in place in C++11.
The factor 2 is probably never reached on realistic code, but the
difference should still be noticable in real code using small numbers.
Note that by default, C++11 assumes that destructors don't throw. If we
want to allow users to throw from their deallocation functions (why?), we
might want to specify it (which disables the optimization discussed here).
More information about the gmp-devel