-1
\$\begingroup\$

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;
}
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – SuperBiasedMan Feb 23 '16 at 9:31
  • \$\begingroup\$ Thanks for the welcoming. yes, this is a code for learning purposes. sorry for the off topic. ill try over there. \$\endgroup\$ – proton Feb 23 '16 at 11:17
  • \$\begingroup\$ 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. \$\endgroup\$ – SuperBiasedMan Feb 23 '16 at 11:20
  • \$\begingroup\$ right now my code doesnt seem to work, so it should certainly be improved... it's an homework assignment, if it helps. \$\endgroup\$ – proton Feb 23 '16 at 12:07
  • \$\begingroup\$ 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. \$\endgroup\$ – nwp Feb 24 '16 at 11:54
3
\$\begingroup\$

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++".

\$\endgroup\$
  • \$\begingroup\$ thanks. But i could not found what i need - i must time a simple opertaion and an empty function, which should be in the same file as measureTimes() is (and besides, moving foo to another file actually reduced times so far). \$\endgroup\$ – proton Feb 23 '16 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.