Note that there are some explanatory texts on larger screens.

plurals
  1. POFramelimiter | Why are there extra milliseconds?
    primarykey
    data
    text
    <p>After a lot of testing with this thing, I still can't figure out why there are extra milliseconds appended to the millisecond limit.</p> <p>In this case, the whole running loop should last 4000ms, and then print 4000 followed by some other data, however it is always around 4013ms.</p> <p>I currently know that the problem isn't the stress testing, since without it, it's still at around 4013ms. Besides, there's is a limit for how long the stress testing can take, and the time is justified by how much rendering is able to be done in the remaining time. I also know that it isn't "SDL_GetTicks" including the time I'm initialising variables, since it only starts timing when it is first called. It's not the time it takes to call the function either, because I tested this with a very lightweight nanosecond timer as well, and the result is the same.</p> <p>Here's some of my results, that are printed at the end:</p> <ul> <li>4013 100 993 40</li> <li>4013 100 1000 40</li> <li>4014 100 1000 40</li> <li>4012 100 992 40</li> <li>4015 100 985 40</li> <li>4013 100 1000 40</li> <li>4022 100 986 40</li> <li>4014 100 1000 40</li> <li>4017 100 993 40</li> </ul> <p>Unlike the third column (the amount of frames rendered), the first column shouldn't vary by much more than the few nanoseconds it took to exit the loops and such. Meaning it shouldn't even show a difference since the scope of the timer is milliseconds in this case. I recompiled between all of them, and the list is pretty much continues the same.</p> <p>Here's the code:</p> <pre><code>#include &lt;iostream&gt; #include &lt;SDL/SDL.h&gt; void stress(int n) { n = n + n - n * n + n * n; } int main(int argc, char **argv) { int running = 100, timestart = 0, timestep = 0, rendering = 0, logic = 0, SDL_Init(SDL_INIT_EVERYTHING); while(running--) { // - Running loop timestart = SDL_GetTicks(); std::cout &lt;&lt; "logic " &lt;&lt; logic++ &lt;&lt; std::endl; for(int i = 0; i &lt; 9779998; i++) { // - Stress testing if(SDL_GetTicks() - timestart &gt;= 30) { // - Maximum of 30 milliseconds spent running logic break; } stress(i); } while(SDL_GetTicks() - timestart &lt; 1) { // - Minimum of one millisecond to run through logic ; } timestep = SDL_GetTicks() - timestart; while(40 &gt; timestep) { timestart = SDL_GetTicks(); std::cout &lt;&lt; "rendering " &lt;&lt; rendering++ &lt;&lt; std::endl; while(SDL_GetTicks() - timestart &lt; 1) { // - Maximum of one rendering frame per millisecond ; } timestep += SDL_GetTicks() - timestart; } } std::cout &lt;&lt; SDL_GetTicks() &lt;&lt; " " &lt;&lt; logic &lt;&lt; " " &lt;&lt; rendering &lt;&lt; " " &lt;&lt; timestep &lt;&lt; std::endl; SDL_Quit(); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
 

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