performance - Same function? runs about 10 time slower with GMP(C++) -
i wrote mandelbrot zoom in c++ zoom limited due floating point inaccuracy. that's why wrote whole thing againg gmp library.
but have problems performance. i'm new gmp maybe messed few things:
here original code:
int iterate(double xp, double yp, int iterations){ double length = 1; double x = 0; double y = 0; double r; for(int = 0; < 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 gmp (i think it's same), added 2 variables, temp , temp2, store values calculation shouldn't make 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 = 0; < 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 can me.
i added 2 variables, temp , temp2, store values calculation shouldn't make 10 times slower
operations on float
, double
handled hardware. whereas operations on gmp types handled gmp library in software.
gmp values require more memory store , memory bottleneck.
Comments
Post a Comment