performance question

DTAshley at DTAshley at
Fri Apr 30 06:29:58 CEST 2004

Hi Nathan,

In short, I would just move all the variables involved to be external to the 
functions (i.e. global), then initialize them once, do all the stuff you have 
to do, then clear them once (optional).  That way you can still have a 
function call, but without all the new and destroy-type overhead.

In effect, change the variables from storage class automatic to another 
storage class so they have permanent lifetime.

BTW, welcome to the real world.  There is often a tradeoff between 
performance and the management of complexity.  Better performance comes at the cost of 
increased complexity.  Tools can help you out a bit (inline functions, etc.), 
but in the end you still can encounter scenarios that no compiler is smart 
enough to handle.

BTW, I don't think inline functions will help you out much.  The real 
performance hit will be malloc() and free(), which doesn't go away by inlining.  The 
function call is probably not a tangible factor.  You've gotta change the 
lifetime of those variables to avoid repeated deallocation and allocation.

To answer this question:

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?

a)The compiler won't figure it out, and

b)Inlining won't help you unless you remove the repeated allocation and 


In a message dated 4/29/2004 11:06:24 PM Eastern Daylight Time, 
nmoore at writes:

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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: /list-archives/gmp-discuss/attachments/20040430/8085f5b2/attachment.htm

More information about the gmp-discuss mailing list