Problem 11
From CSI 702
Contents |
1. Problem 11
1.1. Description
Which of the following will execute faster?
increment=xmax/large_number do i=1,large_number x(i)=i*increment enddo
increment=xmax/large_number sum=increment do i=1,large_number x(i)=sum sum=sum+increment enddo
1.2. Expectation
The two routines preform basically the same function, however the first performs a multiplication calculation at each iteration whereas the second uses addition. Since multiplication is a more complicated operation, I would expect the first routine to go more slowly, and hopefully a good compiler would figure this out anyway.
1.3. Code
#include <stdio.h> #include <stdlib.h> #include <math.h> #define XMAX 100 #define BIGNUM 1000000000 void case1(); void case2(); int main( int argc , char* argv[] ) { if ( argc < 2 ) { printf("Usage: problem2 [1|2]\n"); exit(-1); } int caseNum = atoi( *++argv ); if ( caseNum == 1 ) { case1( BIGNUM ); } else if ( caseNum == 2 ) { case2( BIGNUM ); } else { printf("Usage: problem2 [1|2]\n"); exit(-1); } exit(0); } void case1() { // increment=xmax/large_number float inc = XMAX/BIGNUM; // init array float *x; x = (float *)malloc(sizeof(float)*BIGNUM); int i; // do i=1,large_number for(i = 0; i < BIGNUM; i++) { // x(i)=i*increment x[i] = i*inc; } // enddo return; } void case2() { // increment=xmax/large_number float inc = XMAX/BIGNUM; // init array float *x; x = (float *)malloc(sizeof(float)*BIGNUM); // sum=increment float sum = inc; int i; // do i=1,large_number for(i = 0; i < BIGNUM; i++) { // x(i)=sum x[i] = sum; // sum=sum+increment sum = sum + inc; } // enddo return; }
1.4. Profile and Timing
Both cases were run 5 times at each compiler optimization level, using the perl script found here.
The timing results can be found here.
1.5. Comments
Case 1 does not benefit substantially until the O3 optimization level, at which point it is competitive with the performance of case 2. This is likely due to
In case 2, just one level of compiler optimization yields an approximately tenfold performance gain, with little gain from subsequent optimization levels. This is likely due to
1.6. Contributors
Steven Baehr

