Note that there are some explanatory texts on larger screens.

plurals
  1. POC++/POSIX how to get a millisecond time-stamp the most efficient way?
    text
    copied!<p>I'm using a open-source library for i2c bus actions. This library frequently uses a function to obtain an actual time-stamp with millisecond resolution. </p> <p>Example Call:</p> <pre><code>nowtime = timer_nowtime(); while ((i2c_CheckBit(dev) == true) &amp;&amp; ((timer_nowtime() - nowtime) &lt; I2C_TIMEOUT)); </code></pre> <p>The application using this i2c library uses a lot of CPU capacity. I figured out, that the running program the most time is calling the function <code>timer_nowtime()</code>.</p> <p>The original function: </p> <pre><code>unsigned long timer_nowtime(void) { static bool usetimer = false; static unsigned long long inittime; struct tms cputime; if (usetimer == false) { inittime = (unsigned long long)times(&amp;cputime); usetimer = true; } return (unsigned long)((times(&amp;cputime) - inittime)*1000UL/sysconf(_SC_CLK_TCK)); } </code></pre> <p>My aim now is, to improve the efficiency of this function. I tried it this way:</p> <pre><code>struct timespec systemtime; clock_gettime(CLOCK_REALTIME, &amp;systemtime); //convert the to milliseconds timestamp // incorrect way, because (1 / 1000000UL) always returns 0 -&gt; thanks Pace //return (unsigned long) ( (systemtime.tv_sec * 1000UL) + (systemtime.tv_nsec // * (1 / 1000000UL))); return (unsigned long) ((systemtime.tv_sec * 1000UL) + (systemtime.tv_nsec / 1000000UL)); </code></pre> <p>Unfortunately, I can't declare this function <em>inline</em> (no clue why). </p> <p>Which way is more efficient to obtain an actual timestamp with ms resolution? I'm sure there is a more per-formant way to do so. Any suggestions? </p> <p>thanks. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload