proton
proton

Reputation: 431

c++ measuring execution time

EDIT: the code was modified following the advices givven to me.

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).

  1. The code compiles and seeme 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 take even a little bit more time.

  2. Another issue is that the execution time do not seem to be affected by the number of itterrations - it could be 100K or 100M but the times are basically the same. Also, if I pick a number over one billion, it seeme the execuition time decreases.

i could not find on google what i need - i must time a simple opertaion and an empty function, which should be in the same file as measureTimes() is, or at least - each measuring function should be contained wholy in a single file (and besides, moving foo() to another file actually reduced times so far

right now, this is the output of my program:

instructionTimeNanoSecond: 1.9

functionTimeNanoSecond: 1.627

    #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
    #define  TO_MICRO 1000000
    #define  ROLL 10

    using namespace std;

    int main()
    {
        unsigned int iterations = (unsigned int) pow( 10, 9);

        measureTimes( iterations, iterations, 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);

    int x=0;
    for( int i = 0; i < iterations/ROLL; i++ )
    {
        x=x+1;
        x=x+1;
        x=x+1;
        x=x+1;
        x=x+1;
        x=x+1;
        x=x+1;
        x=x+1;
        x=x+1;
        x=x+1;
    }

    timeval end;
    gettimeofday(&end, NULL);

    timeval diff;
    timersub(&end, &start, &diff);

   // double micro_seconds =(double) (end.tv_usec - start.tv_usec);

    double ret =((double) diff.tv_sec*TO_MICRO + 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/ROLL; i++ )
    {
        foo();
        foo();
        foo();
        foo();
        foo();
        foo();
        foo();
        foo();
        foo();
        foo();
    }

    timeval end;
    gettimeofday(&end, NULL);

    timeval diff;
    timersub(&end, &start, &diff);

    //double micro_seconds = (double)( (end.tv_sec - start.tv_sec)*TO_MICRO+(end.tv_usec - start.tv_usec));

    double ret =((double) diff.tv_sec*TO_MICRO + diff.tv_usec) / ((double) iterations);

    return ret * TO_NANO;
}

void foo()
{
    return;
}

Upvotes: 0

Views: 277

Answers (3)

Murphy
Murphy

Reputation: 3999

The magic word you're looking for is profiling. It usually is (and should) be supported by your compiler.

Upvotes: 0

Sam Daniel
Sam Daniel

Reputation: 1902

You are always calculating the average.. which should be more or less the same for the no of iterations

double ret = diff.tv_usec / ((double) iterations);

Upvotes: 0

Pooya
Pooya

Reputation: 6136

In this case compiler is optimizing your code and probably doesn't even execute the for loop because the code inside 1+1 is not affecting the rest of the program.

If you do something like:

int x = 0;
for(int i=0;i<iteration;i++)
   x = x + 1;
cout << x;

you will get more real results

Upvotes: 1

Related Questions