title: Best suitable Timer for correct measurement of cache access time
body: <p>There exist so many timers which can be used to find time taken to execute a task or a program takes to complete its execution for example.</p>
  
  <p>I would like to find the access time of L2 cache set by set by accessing array elements loaded into cache . To find the access time of all sets of L2 cache set by set time calculation , I have tried different options like - <strong>rdtsc(), gettimeofday ()</strong>, but not getting exact result as expected .</p>
  
  <p><strong>I am using intel i7 machine in linux with gcc 4.8.</strong> What is the best timer to calculate the cache access time with more accuracy.</p>
  
  <p>my approach is as follow</p>
  
  <pre><code>for ( j=0; j&lt;100000;j++
  {
  
  for ( set_no = 0 ; set_no&lt; 4096; set_no ++)
  { 
  
  start= rdtsc()
  {
  access all blocks of a set = set_no;
  }
  end = rdtsc()
  
  
  time_taken[set_no]+= end -start ;
  }
  }
  
  for(set_no=0;set_no&lt;4096;set_no++)
  printf(" time needed for access set %d = %llu", set_no, time_taken[set_no]/100000);
  </code></pre>
  
  <p>I would like to know whether BEST/SUITABLE Timer available in C or C++ ?</p>
  
  <p><strong>I need to prevent out-of_order execution in i3/i7 to get correct time measurement, so</strong> *<em>SERIALIZATION of timer is required.</em>* Which timer should I use to get correct result with SERIALIZED OUTPUT. rdtsc() has OPTION to serialized the timer output, DOES other timer also have the same ?</p>
  
  <p>should I use other option to calculate time to get better result ? Thanks in advance. </p>