Joel February 2016

Same function? runs about 10 time slower with GMP(C++)

I wrote a Mandelbrot zoom in c++ but the zoom was limited due to floating point inaccuracy. That's why I wrote the whole thing againg with the GMP library.

But now I have problems with the performance. I'm new to GMP so maybe I just messed up a few things:

Here is the original code:

int iterate(double xp, double yp, int iterations){
double length = 1;
double x = 0;
double y = 0;
double r;
for(int i = 0; i < iterations && length <= 2; i++){
    double xTemp = x;
    //calculate real part
    x = (x*x)+xp-(y*y);
    //calculate imaginary part
    y = 2*xTemp*y+yp;
    //calculate lenth
    length = sqrt(x*x+y*y);
    r = i+1;
}
if(length > 2)
    return r;
return 0;

}

Same function with GMP (I think it's the same), I added two variables, temp and temp2, to store values for calculation but this shouldn't make it 10 times slower:

int iterateGMP(mpf_t xpGMP, mpf_t ypGMP, int iterations){
double r;

mpf_set_default_prec (20);

mpf_t length;
mpf_init(length);
mpf_set_d(length, 1);
mpf_t x;
mpf_init(x);
mpf_set_d(x, 0);
mpf_t y;
mpf_init(y);
mpf_set_d(y, 0);
mpf_t xTemp;
mpf_init(xTemp);
mpf_t TempGMP;
mpf_init(TempGMP);
mpf_t Temp2GMP;
mpf_init(Temp2GMP);


for(int i = 0; i < iterations && mpf_cmp_ui(length, 2)<0; i++){
    mpf_set(xTemp, x);

    //calculate real part
    mpf_mul(TempGMP, x, x);
    mpf_add(TempGMP, TempGMP, xpGMP);
    mpf_mul(Temp2GMP, y, y);
    mpf_sub(x, TempGMP, Temp2GMP);

    //calculate imaginary part
    mpf_mul(TempGMP, xTemp, y);
    mpf_mul_ui(TempGMP, TempGMP, 2);
    mpf_add(y, TempGMP, ypGMP);

    //calculate length
    mpf_mul(TempGMP, x, x);
    mpf_mul(Temp2GMP, y, y);
    mpf_add(TempGMP, TempGMP, Temp2GMP);
    mpf_sqrt(length, TempGMP);
    r = i+1;
}
if(mpf_cmp_ui(length, 2) > 0){
    return r;
}
return 0;

}

I hope someone can help me.

Answers


Maxim Egorushkin February 2016

I added two variables, temp and temp2, to store values for calculation but this shouldn't make it 10 times slower

Operations on float and double are normally handled by the hardware. Whereas operations on GMP types are handled by GMP library in software.

GMP values also require more memory to store and memory is often the bottleneck.

Post Status

Asked in February 2016
Viewed 3,848 times
Voted 10
Answered 1 times

Search




Leave an answer