tags:

views:

229

answers:

1

i try to use gprof command: gprof -s executable.exe gmon.out gmon.sum to merge profiling data gethered from 2 runs of my prog. but the following line appears:

gprof: out of memory allocating 3403207348 bytes after a total of 196608 bytes

my prog is quite simple(just one for loop), if i run once, the run time is too short( it shows 0.00s) for gprof to record.

In cygwin, i do the following steps:

1.gcc -pg -o fl forAndWhilLoop.c

2.fl (run the prog)

3.mv gmon.out gmon.sum

4.fl (run the prog)

5.gprof -s fl.exe gmon.out gmon.sum

6.gprof fl.exe gmon.sum>gmon.out

7.gprof fl.exe

My program:

int main(void)
{
    int fac=1;
    int count=10;
    int k;

    for(k=1;k<=count;k++)
    {
        fac = fac * k;
    }

    return 0;
}

so can anyone help me with this problem? thx!

A: 

If all you want is to time it, on my machine it's 105ns. Here's the code:

void forloop(void){ 
    int fac=1; 
    int count=10; 
    int k; 

    for(k=1;k<=count;k++) 
    { 
        fac = fac * k; 
    } 
} 

int main(int argc, char* argv[])
{
    int i;
    for (i = 0; i < 1000000000; i++){
        forloop();
    }
    return 0;
}

Get the idea? I used a hand-held stopwatch. Since it runs 10^9 times, seconds = nanoseconds.

Unrolling the inner loop like this reduced the time to 92ns;

int k = 1;
while(k+5 <= count){
    fac *= k * (k+1) * (k+2) * (k+3) * (k+4);
    k += 5;
}
while(k <= count){
    fac *= k++;
}

Switching to Release build from Debug brought it down to 21ns. You can only expect that kind of speedup in an actual hotspot, which this is.

Mike Dunlavey