performance question

Nathan Moore nmoore at
Fri Apr 30 05:05:14 CEST 2004


I'm currently using gmp to compute the value of a rather delicate 
polynomial many times.  To stay sane I wrote the c program in a modular 
fashion so that the delicate gmp stuff is in a function called from 
main.  What I'm wondering is if calling the function many many times 
(the function creating, initializing, and at the end, clearing the 
mpq_t variables over and over) is stupid in terms of performance costs. 
  I've thought about re-writing the program to just have one function 
and avoid this initialization cost, but that seems really hard to read 
(about 1000 lines).

The code is roughly as follows,

main() {

	//do some initialization stuff, including
	mpq_t b[100];
	for(i=0;i<100;i++) { mpq_init(b[i]); }

	// main iteration, called function includes many mpq functions
	for(i=0;i<10000;i++) {
		for(j=1;j<100;j++) {
			term[j] = compute_value(b, j);

	// do some clean up stuff
	for(i=0;i<100;i++) { mpq_clear(b[i]); }


double compute_value(mpq_t b[], int j) {

	// declare and initialize 15 mpq_t variables
	mpq_t a1,a2,a3...a15;
	mpq_init(a1); .... mpq_init(a15);

	// perform a bunch of mpq math operations (add, multiply, compare etc)
	// final operations use passed in b[] to create returned double value, 

	// clean up
	mpq_clear(a1); .... mpq_clear(a15);
	return result;

To reiterate, will a compiler figure out that mpq_init and mpq_clear 
iterated each time the compute_value() function is called is stupid and 
inline the function for me, or should I inline the functions myself for 
a noticeable performance benefit?

thanks,  all suggestions are welcome!

Nathan Moore
University of Minnesota Physics

More information about the gmp-discuss mailing list