DivRem is 2x slower than cpp_int

Andy borucki.andrzej at gmail.com
Wed Oct 5 15:55:45 UTC 2016


I test under Linux. Multiplications are identical as cpp_int but divide
with remainder is two times slower:
#include <iostream>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/gmp.hpp>
#include <chrono>

using namespace boost::multiprecision;
using namespace std;

#define itype mpz_int
//#define itype cpp_int

class stoper
{
public:
chrono::time_point<chrono::high_resolution_clock> a, b;
void start() { a = chrono::high_resolution_clock::now(); }
void stop() { b = chrono::high_resolution_clock::now(); }
double duration()
{
chrono::duration<double> elapsed_seconds = b - a;
return elapsed_seconds.count();
}
};

void test(const itype n)
{
itype x = n;
itype i, e;
i = 3;
e = sqrt(x);
while (i <= e) {
if ((x % i) == 0) break;
i+=2;
}
}
int main()
{
itype a("3448409664461");//prime
double best = INFINITY;
for (int i=0; i<100; i++)
    {
        stoper st;
        st.start();
        test(a);
        st.stop();
        best = min(best, st.duration());
    }
    printf("%f\n", best);
    return 0;
}

My times: GMP: 0.05 s, cpp_int : 0.024 s


More information about the gmp-discuss mailing list