0

I am trying to measure the time of atomics operations like bitwise for example. The problem I had is that I can't just compute 0&1, because the IDE doing optimisation and ignoring this command, so I had to use assignment

num = 0&1.

So to get the accurate time of the operation without the assignment I was checking the time it takes to do an only assignment, I did that with x=0; and return at the end something like this

return assign_and_comp - assign_only;

The problem is that I'm getting negative results pretty frequently.

Is it possible that num=0&1 cost less then x=0 ?

I cant use any time measuring function except gettimeofday() unfortunately

I've saw This soution , first im forced to use gettimeofday() but the most importent thing is that im mesuaring in the same way, geting the time before and after the operationg, and returning the diff. BUT, i'm trying to isolate the assigment from the operationg, and this is not what they are doing in the soultion.

This is my full code.

#include <iostream>
#include <sys/time.h>
#include "osm.h"

#define NUM_ITERATIONS 1000000
#define SECOND_TO_NANO 1000000000.0
#define MICRO_TO_NANO 1000.0
using namespace std;


//globals variabels
struct timeval tvalBefore, tvalAfter;
double assign_only = 0.0;

int main() {
    osm_init();
    cout << osm_operation_time(50000) << endl;
    return 0;
}

int osm_init(){
    int x=0;
    gettimeofday(&tvalBefore,NULL);
    for (int i=0; i<NUM_ITERATIONS; i++){
        x = 0;
    }
    gettimeofday(&tvalAfter,NULL);
    assign_only  = ((tvalAfter.tv_sec-tvalBefore.tv_sec)*SECOND_TO_NANO+
                    (tvalAfter.tv_usec-tvalBefore.tv_usec)*MICRO_TO_NANO)/NUM_ITERATIONS;
    return 0;
}

double osm_operation_time(unsigned int iterations){
    volatile int num=0;
    gettimeofday(&tvalBefore,NULL);
    for (int i=0; i<iterations; i++){
         num = 0&1;
    }
    gettimeofday(&tvalAfter,NULL);
    double assign_and_comp = ((tvalAfter.tv_sec-tvalBefore.tv_sec)*SECOND_TO_NANO+
                                (tvalAfter.tv_usec-tvalBefore.tv_usec)*MICRO_TO_NANO)/iterations;
    return  assign_and_comp-assign_only;
}
Community
  • 1
  • 1
limitless
  • 669
  • 7
  • 18
  • Possible duplicate of [Measuring execution time of a function in C++](http://stackoverflow.com/questions/22387586/measuring-execution-time-of-a-function-in-c) – UnholySheep Mar 10 '17 at 09:30
  • @UnholySheep I've added a description why i think this isnt a duplicate. – limitless Mar 10 '17 at 09:37
  • `gettimeofday` is unsuited for measuring execution time of instructions. It is not intended for that purpose and that is why you occasionally get negative results. - see this question for more details: http://stackoverflow.com/questions/88/is-gettimeofday-guaranteed-to-be-of-microsecond-resolution – UnholySheep Mar 10 '17 at 09:40
  • @UnholySheep Thank you but Im not allowed to use any other time measuring function. – limitless Mar 10 '17 at 09:44
  • So you'll have to run for several minutes, If you can use clock() then you only need a tenth of a second or so to iron out initialisation factors and get a fair result. – Malcolm McLean Mar 10 '17 at 09:52
  • @MalcolmMcLean even with 1000000000 iterations im still getting negative result. – limitless Mar 10 '17 at 09:58

0 Answers0