counting duration with std::chrono gives 0 nanosecond when it should take long
<p>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 : </p>

<pre><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&lt;10000;c++){ // even if i put  1000000000 it will take 0 nanosec !!!!!
        f.z = x+y;
        f.z += z-x+y;
    }
   std::cout&lt;&lt;"\ntoken time : "&lt;&lt; std::chrono::duration_cast&lt;std::chrono::nanoseconds&gt;(std::chrono::system_clock::now()-t1).count()&lt;&lt;std::endl;;
}
</code></pre>

<p>output :</p>

<pre><code>token time : 0
</code></pre>

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

<p><strong>the answer</strong> : 
as WhiZTiM said , the compiler is removing the loop because it does nothing useful ( thanks gcc &lt;3 ) , but we really don't want that to happen when we are testing to algorithms to see wich one is faster on different compilers ( and not this spesific one ) , to do so we can insert an asm line into the loop . <code>asm("")</code> , an empty asm , anywhere in the loop . That will tell the compiler that there is some low level operations that he can't optimize ! , or we can use the volitile keyword for any variable used in the loop that prevents the compiler from doing any optimization related to that variable  . thanks everyone i hope this helps</p>