Question

I am tasked with two programs and this is the second one. The first program involved no calculation() function and to time the program when it started and finished. My computer will display anything from .523 seconds to .601 seconds.

The second task was to create an inline function for the calculation and I believe that I have done it wrong because it is not faster. I am not sure if I made the calculation function right because it includes display information, or if the inline function should focus only on the multiplication. Either way pulling the arrays out of main and into a function is not faster.

Is the compiler just ignoring it?

#include <ctime>
#include <iostream>
using namespace std;

  inline int calculation(){
  int i;
  double result[10000];

double user[10000];
   for(i=0; i<10000; i++){
      user[i]=i+100;    
   }

    double second[10000];
   for(i=0; i<10000; i++){
      second[i]=10099-i;    
   }

    for (i = 0; i < 10000; i++){
       result[i] = user[i] * second[i];
    }

    for (i = 0; i < 10000; i++){
       cout << user[i] << " * " << second[i] << " = " << result[i] << '\n';
       }
   }


    int main() {

    time_t t1 = time(0);                  // get time now
    struct tm * now = localtime( & t1 );
    cout << "The time now is: ";
    cout  << now->tm_hour << ":" << now->tm_min << ":" << now->tm_sec << endl;

    clock_t t;                        // get ticks
    t = clock();
    cout << " Also calculating ticks...\n"<<endl;

    calculation();                    // inline function

    time_t t2 = time(0);                  // get time now
    struct tm * now2 = localtime( & t2 );
    cout << "The time now is: ";
    cout << now2->tm_hour << ":" << now2->tm_min << ":" << now2->tm_sec << endl;
    time_t t3= t2-t1;

    cout << "This took me "<< t3 << " second(s)" << endl;  // ticks
    t = clock() - t;
    float p;
    p = (float)t/CLOCKS_PER_SEC;
    cout << "Or more accuratley, this took " << t << " clicks" 
    << " or " << p << " seconds"<<endl;

    }
Was it helpful?

Solution

Is the compiler just ignoring it?

Most probably, yes. It could be doing that for two reasons:

  • You're compiling in debug mode. In debug mode all inline keywords are ignored to facilitate debugging.
  • It's ignoring it because the function is far too long for an inline function, and uses far too much stack space to safely inline, and is only invoked once. The inline keyword is a compiler HINT, not a mandatory requirement. It's the programmer's way of recommending the compiler to inline the function, just like a compiler in release mode will frequently inline functions on its own to increase performance. If it only sees negative value it won't comply.

Also, given the single invocation, it's highly unlikely that you'll even see differences no matter if it works or not. A single native function call is much easier on the CPU than a single task switch at the OS level.

OTHER TIPS

You should disable optimization to verify if what you do has any effect, because there are good chances that the compiler is already inlining the function by itself.

Also, if you want to know exactly what your code does, you should compile with the -s flag in g++, and look at the assembly generated by the compiler for your program. This will remove all uncertainty about what the compiler is doing to your program.

I would not make the function inlined and define arrays as static. For example

int calculation(){
  int i;
  static double result[10000];

static double user[10000];
   for(i=0; i<10000; i++){
      user[i]=i+100;    
   }

    static double second[10000];
   for(i=0; i<10000; i++){
      second[i]=10099-i;    
   }

    for (i = 0; i < 10000; i++){
       result[i] = user[i] * second[i];
    }

    for (i = 0; i < 10000; i++){
       cout << user[i] << " * " << second[i] << " = " << result[i] << '\n';
       }
   }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top