Tuesday, 9 July 2013

start time and end time of a function

Finding difference between inline function & Non_inline function

#include <iostream>
#include <ctime>
#include <stdio.h>
using namespace std;

inline void f1 (double i = 0)
  {
    clock_t start,end;
    start = clock();
    cout << "start time ==> " << start << endl;
     for(double j = 1;j<=i; j++)
      {
        double res = (i+j)+(i/j)+(i*j); 
      }
    end = clock();
    cout << "end time ==> " << end << endl;
    double total_time_taken = end - start;
    cout << "inline f1 ==> " << i << "  Time taken ==> "<< total_time_taken << endl;
  }

 void f2 (double i = 0)
  {
   clock_t start,end;
    start = clock();
    cout << "start time ==> " << start << endl;
    for(double j = 1;j<=i; j++)
      {
        double res = (i+j)+(i/j)+(i*j); 
      }
    end = clock();
    cout << "end time ==> " << end << endl;
    double total_time_taken = end - start;
    cout << "Non_inline f2 ==> " << i << "  Time taken ==> "<< total_time_taken << endl;
  }


int main()
{
 f1(1000000);
 f2(1000000);
 f1(1000000);
 f2(1000000);
 return 1;
}

<<<=================OUT PUT ==================>>

start time ==> 0
end time ==> 10000
inline f1 ==> 1e+06  Time taken ==> 10000
start time ==> 10000
end time ==> 20000
Non_inline f2 ==> 1e+06  Time taken ==> 10000
start time ==> 20000
end time ==> 30000
inline f1 ==> 1e+06  Time taken ==> 10000
start time ==> 30000
end time ==> 40000
Non_inline f2 ==> 1e+06  Time taken ==> 10000
<<<=================OUT PUT ==================>>