title: High-resolutin (100 nsec) timing on Linux/C
body: <p>I use Raspian on the Raspberry B+ to get 1700 nsec (+- 10%) pulses on a GPIO output. Thus, I need a high-resolution wallclock timer. There are several references to <code>clock_gettime</code> for high-resolution timing (e.g. <a href="https://stackoverflow.com/questions/538609/high-resolution-timer-with-c-and-linux">1</a>, <a href="https://stackoverflow.com/questions/6749621/high-resolution-timer-in-linux">2</a>). However, I get 1. only a microsecond resolution and 2. not sufficient minimum time with this short code:</p>
  
  <pre><code>int start_time, current_time, elapsed_time;
  struct timespec resolution;
  
  clock_gettime(CLOCK_MONOTONIC, &amp;resolution);
  start_time = Resolution.tv_nsec;
  
  clock_gettime(CLOCK_MONOTONIC, &amp;resolution);
  current_time = resolution.tv_nsec;
  
  elapsed_time = current_time - start_time;
  if(elapsed_time &lt; 0) {
    elapsed_time = elapsed_time + 1000000000; //in case clock_gettime wraps around
  }
  
  printf("%i\n", elapsed_time);
  </code></pre>
  
  <p>The result is 3000 (nanoseconds), i.e. even this shortest possible piece of code takes too much time. If I add some time-consuming code, the next greater result is 4000.</p>
  
  <p>How I can I get a wallclock timer that will result in at least 100 nsec resolution and a smallest possible time of less than 1700 nsec? That the Raspberry Pi can do faster (100 nsec pulses with WiringPi) shows the <a href="http://codeandlife.com/2012/07/03/benchmarking-raspberry-pi-gpio-speed/" rel="nofollow noreferrer">GPIO Benchmark</a>. I am aware that additional electronics (monoflop) can help me but I hope to solve the problem in a simpler way. Thank you.</p>