title: What's the best timing resolution can i get on Linux
body: <p>I'm trying to measure the time difference between 2 signals on the parallel port, but first i got to know how much accurate and precise is my measuring system (AMD Athlon(tm) 64 X2 Dual Core Processor 5200+ × 2) on SUSE 12.1 x64.</p>
  
  <p>So after some reading i decide to use clock_gettime(), first i get the clock_getres() value using this code:</p>
  
  <pre><code>/*
   * This program prints out the clock resolution.
   */
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;time.h&gt;
  
  int main( void )
    {
      struct timespec res;
  
      if ( clock_getres( CLOCK_REALTIME, &amp;res) == -1 ) {
        perror( "clock get resolution" );
        return EXIT_FAILURE;
      }
      printf( "Resolution is %ld nano seconds.\n",
            res.tv_nsec);
      return EXIT_SUCCESS;
    }
  </code></pre>
  
  <p>and the out was: 1 nano second. And i was so happy!!</p>
  
  <p>But here is my problem, when i tried to check that fact with this other code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;time.h&gt;
  using namespace std;
  
  timespec diff(timespec start, timespec end);
  
  int main()
  {
      timespec time1, time2, time3,time4;
      int temp;
      time3.tv_sec=0;
      time4.tv_nsec=000000001L;
      clock_gettime(CLOCK_REALTIME, &amp;time1);
          NULL;
      clock_gettime(CLOCK_REALTIME, &amp;time2);
      cout&lt;&lt;diff(time1,time2).tv_sec&lt;&lt;":"&lt;&lt;diff(time1,time2).tv_nsec&lt;&lt;endl;
      return 0;
  }
  
  timespec diff(timespec start, timespec end)
  {
      timespec temp;
      if ((end.tv_nsec-start.tv_nsec)&lt;0) {
          temp.tv_sec = end.tv_sec-start.tv_sec-1;
          temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
      } else {
          temp.tv_sec = end.tv_sec-start.tv_sec;
          temp.tv_nsec = end.tv_nsec-start.tv_nsec;
      }
      return temp;
  }
  </code></pre>
  
  <p>this one calculate the time between the two calls of clock_gettime, the time3 and time4 are declared but not used in this example because i was doing tests with them.</p>
  
  <p>The output in this example is fluctuating between 978 and 1467 ns. both numbers are multiples of 489, this make me think that 489 ns is my REAL resolution. far far from the 1 ns obtained above.</p>
  
  <p>My question: is there ANY WAY of getting better results? am i missing something?</p>
  
  <p>I really need at least 10ns resolution for my project. Come on! a GPS can get better resolution than a PC??</p>