Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>However, I'm doing this quite often and wonder if it could become a bottleneck.</p> </blockquote> <p>First things first. If you think this will be a bottleneck, <em>profile</em> the code in question and find out for sure.</p> <p>If, (and only if) this is your bottleneck, then work on improving it.</p> <p>Now, on to your improvement options:</p> <p><code>1.</code> You may not need to convert to milliseconds right away. If you are simply collecting data, just store the full 64-bit number returned from <code>gethrtime()</code> and be done with it. Anything that a human needs to read can be post-processed at a later time, or on a much less agressive update frequency.</p> <p><code>2.</code> If you are timing some repetitive event, you can try performing the division on the <em>difference</em> between two calls, which should be <em>very</em> small if you are calling <code>gethrtime()</code> often enough to have a bottleneck:</p> <pre><code>static hrtime_t oldtime; hrtime_t newtime = gethrtime(); int milliseconds = fastDivByOneMillion((UI32)(newtime - oldtime)); oldtime = newtime; </code></pre> <p><code>3.</code> You can implement <code>fastDivByOneMillion()</code> as a multiplication and a division by a power of 2:</p> <pre><code>int fastDivByOneMillion(UI32 nanoseconds) { return (int)((UI64)nanoseconds * 4295 &gt;&gt; 32); } </code></pre> <p>Notes:<br> <li> Your compiler can figure out the best way to do <code>&gt;&gt; 32</code> on your hardware. Most of the time, this will be only one or two clock cylces.<br> <li> I used <code>UI32</code> and <code>UI64</code> to represent 32 and 64-bit unsigned numbers. <li> All of this will require more profiling to be sure that it is actually producing a measurable improvement.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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