# Measuring execution time of a simple operation

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;
}

• Welcome to Code Review! Are you only interested in understanding the optimisation here? It doesn't seem like this is actual code you intend to use, which would make it off topic for here but maybe better for Stack Overflow. – SuperBiasedMan Feb 23 '16 at 9:31
• Thanks for the welcoming. yes, this is a code for learning purposes. sorry for the off topic. ill try over there. – proton Feb 23 '16 at 11:17
• To explain a bit clearer, Code Review is about giving feedback and advice on how to improve code. When you're more curious about the implementation or understanding an aspect of the language, Stack Overflow is more useful. Hope that helps, you can read the on topic help page if you'd like to know more. – SuperBiasedMan Feb 23 '16 at 11:20
• right now my code doesnt seem to work, so it should certainly be improved... it's an homework assignment, if it helps. – proton Feb 23 '16 at 12:07
• Code that doesn't work is explicitly off topic here. I'll advertise my own answer that essentially says you cannot do this properly. But that is not your fault, it is the fault of the person who gave you the assignment. To get a useful review you need to at least make it compile (measureTimes is not defined when you use it in main) and actually show the code fatal error: osm.h: No such file or directory. – nwp Feb 24 '16 at 11:54

Compile with -S to produce an assembly listing, and read that assembly listing. This should answer your question. :)
Both loops are getting optimized away completely, since they have no visible side-effects. To find out how to run a proper benchmark in C++, google something like "how to run a proper benchmark in C++".