dimanche 2 octobre 2016

counting duration with std::chrono gives 0 nanosecond when it should take long

I was trying to count the duration taken by a for loop using std::chrono but it gives 0 nanoseconds even if i make the loop take longer by increamenting the bound value , this is the code :

#pragma pack(1) // dont align let's let it take longer
struct Foo{
    int x;
    char c;
    int z;
} ;
void take_time()
{
    Foo f;
    auto t1 = std::chrono::system_clock::now();
    register int c = 0;
    int x=0,y=0,z=1;
    for (c=0;c<10000;c++){ // even if i put  1000000000 it will take 0 nanosec !!!!!
        f.z = x+y;
        f.z += z-x+y;
    }
   std::cout<<"\ntoken time : "<< std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now()-t1).count()<<std::endl;;
}

output :

token time : 0

but when i increament the bound of the loop's counter to very very huge value it suddenly takes forever !! , if i put c<100000000 it takes 0 nanosec but if i add one '0' on the right it takes forever !!

Aucun commentaire:

Enregistrer un commentaire