How to loop n times in 1 second

I am trying to execute a couple of instructions or a function N times in one second. How can i do this in java? Properly...

//in one second while(N) { printf("........."); int x=0; printf("The value of x is "); } 

but the question actually goes a little deeper. I'm trying to draw pixels by hand, and I need the rotation effect per second ... so basically it should execute N times in a second (but this is done endlessly)

early

+7
source share
6 answers

You can never be sure that this will happen exactly N times per second, but it looks like this:

 long taskTime = 0; long sleepTime = 1000/N; while (true) { taskTime = System.currentTimeMillis(); //do something taskTime = System.currentTimeMillis()-taskTime; if (sleepTime-taskTime > 0 ) { Thread.sleep(sleepTime-taskTime); } } 
+5
source

I would flip the problem: don't limit the loop N times per second. Instead, process N units of work evenly distributed over the required time.

That is, calculate how much time has passed from the very beginning (or previous work), interpolate this to the speed of work and perform such work (factor at the beginning / at the previous time and the amount of work that was done). This is the fundamental foundation of many game / animation engines - "delta time" .

Then call yield at the end of each cycle to “be beautiful,” or rather to prevent the use of 99% + processor! The output itself has a minimum resolution of 1 but the effects are usually adequate, especially with appropriate interpolation.

Since the interpolation approach is used, this should work for all N (which can be performed within the allotted time), even if this means performing a larger number N of each cycle. It is also possible that no work can be done by any particular loop for a small N, but yield makes this kind of “extra busy cycle” cheap from the point of view of using CPU 2 .


Here is some pseudo code to print 20 "x" per second, where now returns fractional seconds:

 rate = 20 // per second - "N times per second" done = 0 goal = 1 * rate // same as rate for 1 second start = now() while done < goal: target = floor((now() - start) * rate) work = (target - done) // work might be 0, that okay for 0 upto work: print("x") done += work yield() 

In this case, it is easy to interpolate the start time due to the constant speed formula. The use of "delta time" based on the time elapsed since the last work (or cycle) is similar and suitable when there is no discrete formula, but a little more complicated and can lead to subtle drift errors.


1 The temporal resolution of the actual sleep/yield is implementation dependent and system dependent. For example, it can vary from 1ms on Linux to 10-15ms on windows .

2 In addition to working with the time delta, the sleep period can be changed in accordance with the answer of Dariusz Waver. However, this adds complexity, and a simple yield enough.

+3
source

It’s hard to keep up with the proposed solutions. Instead of calculating the gap between iterations, calculate the absolute time when the next iteration should be performed. This is more accurate since iterations are independent of the previous ones.

 long startTime = System.currentTimeMillis(); long msPerIteration = 1000000/iterationsPerSecond; long i=0; while (true) { // do stuff long msWaiting = startTime + (msPerIteration * ++i) - System.currentTimeMillis(); if (msWaiting > 0) Thread.sleep(msWaiting); } 
+1
source

Change the loop to while(true) .

Check the time in milliseconds before the while . At the end of the while get the time in milliseconds and see if 1000 of them have passed. If so break .

0
source

Pseudocode:

 Create Timer t; t.Start; int counter = N; While ((t.Elapsedtime < 1 Second) AND (N > 0)) { call your_function(); N--; } your_function() { printf("........."); int x=0; printf("The value of x is "); } 
0
source
 long start = System.currentTimeMillis(); while (System.currentTimeMillis() - start <= 1000) { // Your code goes here } 

All you need is that your code will loop for 1000 milliseconds. The number of times this has been done is uncertain and can vary with each run.

0
source

All Articles