Improvement to mpz_nextprime

delta trinity deltatrinity at hotmail.com
Mon Jun 19 13:59:20 CEST 2006


>   I had a quick look at the mpz_nextprime code. While I noticed the new 
>code
>   which is not yet enabled, it surprised me how naïve the current
>   implementation is.
>
>   void
>   mpz_nextprime (mpz_ptr p, mpz_srcptr t)
>   {
>     mpz_add_ui (p, t, 1L);
>     while (! mpz_probab_prime_p (p, 5))
>       mpz_add_ui (p, p, 1L);
>   }
>
>
>   You can easily get a 2x speedup just by doing:
>
>   void
>   mpz_nextprime (mpz_ptr p, mpz_srcptr t)
>   {
>     mpz_add_ui (p, t, 1L);
>     mpz_setbit(p, 0);
>     while (! mpz_probab_prime_p (p, 5))
>       mpz_add_ui (p, p, 2L);
>   }
>
>Have you tested your claim?
>
>I doubt it will any measurable speedup for any
>(non-constructed) case.
>
>--
>Torbjörn

I guess that no practical speedup would be gained for bigger numbers.  It 
depend if mpz_probab_prime_p check for bit zero at the start.  Also have to 
take in account the overhead of adding extra additions, calls, checks for 
the even numbers.

Though, it might be interesting to verify with different numbers size.

Eric




More information about the gmp-discuss mailing list