Is the cycle running at the same speed for all systems?

Whether cyclization in C # is performed at the same speed for all systems. If not, how can I control cycle speed to ensure consistency across all platforms?

+3
source share
4 answers

You can set the minimum time for the time spent on the cycle, for example:

for(int i= 0; i < 10; i++)
{
   System.Threading.Thread.Sleep(100);
   ... rest of your code...
}

A sleep call will require a minimum of 100 ms (you cannot tell what the maximum will be), so your cycle will take at least 1 second to complete 10 iterations.

, Windows , .

+9

. , , , , , ( ), , .

, . . , . , , , .

+7

, . , .

:

int j;
for(int i = 0; i < 100; i++) {
  j = j + i;
}

, , , , . . , . 1 ( ), 6 * 100 ( , , ) 6 * 100 , . . .

. 1 . , 4 /. (), . .

?

, , , .

+6

- Stopwatch , . . :

int noofrunspersecond = 30;
long ticks1 = 0;
long ticks2 = 0;
double interval = (double)Stopwatch.Frequency / noofrunspersecond;
while (true) {
  ticks2 = Stopwatch.GetTimestamp();
  if (ticks2 >= ticks1 + interval) {
    ticks1 = Stopwatch.GetTimestamp();
    //perform your logic here
  }
  Thread.Sleep(1);
}

This will make sure that the logic is executed at specified intervals as long as the system can keep up, so if you try to execute 100 times per second, depending on the logic being executed, the system may not execute this logic 100 times per second. In other cases, this should work fine.

Such logic is good for getting smooth animations that don't accelerate or slow down on different systems, for example.

+2
source

Source: https://habr.com/ru/post/1773050/


All Articles