<p>I wish to calculate the time it took for an API to return a value.
The time taken for such an action is in the space of nanoseconds. As the API is a C++ class/function, I am using the timer.h to calculate the same:</p>
<pre><code>  #include &lt;ctime&gt;
  #include &lt;iostream&gt;

  using namespace std;

  int main(int argc, char** argv) {

      clock_t start;
      double diff;
      start = clock();
      diff = ( std::clock() - start ) / (double)CLOCKS_PER_SEC;
      cout&lt;&lt;&quot;printf: &quot;&lt;&lt; diff &lt;&lt;'\n';

      return 0;
  }
</code></pre>
<p>The above code gives the time in seconds. How do I get the same in nano seconds and with more precision?</p>

In general, for timing how long it takes to call a function, you want to do it many more times than just once. If you call your function only once and it takes a very short time to run, you still have the overhead of actually calling the timer functions and you don't know how long that takes.
For example, if you estimate your function might take 800 ns to run, call it in a loop ten million times (which will then take about 8 seconds). Divide the total time by ten million to get the time per call.