The purpose of the following code is to measure the execution time of a simple operation (1+1) and of a call to a function who does nothing (foo
).
The code compiles and seems to work properly, but the results I am getting are weird. It seems the basic operation requires about the same time as the function call, and most times it takes even a little bit more time.
Another issue is that the execution time does not seem to be affected by the number of iterations - it could be 100K or 100M but the times are basically the same. Also, if I pick a number over one billion, it seems the execution time decreases.
#include <iostream>
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#include <math.h>
#include "osm.h"
#define INVALID_ITERATIONS 0
#define DEFAULT_ITERATIONS 1000
#define HOST_NAME_LEN 100
#define TO_NANO 1000
using namespace std;
int main()
{
unsigned int iterations = (unsigned int) pow( 10, 9);
measureTimes( iterations, iterations);
return 0;
}
void foo();
timeMeasurmentStructure measureTimes (unsigned int operation_iterations,
unsigned int function_iterations)
{
double functionTimeNanoSecond;
functionTimeNanoSecond = osm_function_time( function_iterations);
cout << "functionTimeNanoSecond: " << functionTimeNanoSecond << "\n";;
double instructionTimeNanoSecond;
instructionTimeNanoSecond = osm_operation_time( operation_iterations);
cout << "instructionTimeNanoSecond: " << instructionTimeNanoSecond << "\n";
}
double osm_operation_time(unsigned int iterations)
{
timeval start;
gettimeofday(&start, NULL);
for( int i = 0; i < iterations; i++ )
{
1+1;
}
timeval end;
gettimeofday(&end, NULL);
timeval diff;
timersub(&end, &start, &diff);
double micro_seconds =(double) (end.tv_usec - start.tv_usec);
double ret = diff.tv_usec / ((double) iterations);
return ret * TO_NANO;
}
double osm_function_time(unsigned int iterations)
{
timeval start;
gettimeofday(&start, NULL);
for( int i = 0; i < iterations; i++ )
{
foo();
}
timeval end;
gettimeofday(&end, NULL);
timeval diff;
timersub(&end, &start, &diff);
double micro_seconds = (double) (end.tv_usec - start.tv_usec);
double ret = diff.tv_usec / ((double) iterations);
return ret * TO_NANO;
}
void foo()
{
return;
}
measureTimes
is not defined when you use it inmain
) and actually show the code fatal error: osm.h: No such file or directory. \$\endgroup\$