C # How to loop the maximum number of times

When traversing a very long array or complex calculations for each index, there is a way to get after iterating over the array for as long as possible. Maximum time - maximum time for each frame.

For example:

for(int i = 0; i < 100000; i++){

   do something complicated;

   if(maximum amount of time /*right before the user feels lag*/)
      yield; (come back and resume i where it last yielded)
   }
}
//order does not matter

      

So basically what I want to achieve is a high utilization percentage for the cpu, however I don't want it to go beyond 100% and the user will experience lag

edit: Sorry for a little confusion. A clearer example would be 3D rendering in a program like blender. When the user clicks on the render, it calculates each pixel to determine what color it should be. When you look at the CPU usage, it is close to 100%. however it does not freeze while it calculates pixels, while it calculates the maximum possible

+3


source to share


4 answers


If you are running your code on multiple processors (as implied by the tag multithreading

), there (in the usual case) there is no need to stop the loop from executing in order for your UI to remain responsive. Perform the calculation on one or more background threads and those background threads will update the UI thread accordingly.

is there a way to get after iterating over the array for maximum time

If by profitability you mean just stop (and restart from the beginning of the next frame), then it is a must. You can pass a CancellationToken to your thread and check the cancellation request periodically. You can use a timer at the start of each frame to hide this request, or rather, use an existing mechanism that is already processing at the end of the frame to force the thread to terminate.



If by profitability you mean stopping where I am and resuming at that place at the beginning of the next frame, I would ask why stopping, assuming you have multiple processors. If you must stop, you can use the CancellationToken as before, but just keep track of where you are in the loop, resuming from there, not at the beginning.

So basically what I want to achieve is a high utilization percentage for the cpu, however I don't want it to go beyond 100% and the user will experience lag

You cannot use more than 100% of the CPU usage by definition. To avoid feeling lagging when CPU utilization is high, use thread priorities to ensure that the foreground thread has a higher priority than your background threads.

+5


source


If I am missing something ...



 double MAX_PROCESSTIME = 50.0;

 DateTime loopStart = DateTime.Now();
 for(int i = 0; i < 100000; i++){

   // do something complicated;

   double timePassed = (DateTime.Now() - loopStart).TotalMilliseconds;
   if(timePassed  > MAX_PROCESSTIME) 
   {
       break;
   }
}

      

0


source


A common solution to this problem is to move the work to a separate thread that cannot interrupt the UI, and let the UI or controller thread cancel the job when called.

Another option is that I read somewhere typical people have a perception rate of about 25 milliseconds; two events are supposed to happen simultaneously if they are less than 25 milliseconds apart. Unfortunately I can no longer find the original link, but at least I found a supporting article . You can use this fact to set the timer for about that long and let the process run for as long as you want until the timer goes off. You can also consider an atypical person, especially if your application is in an area serving people who may have more pronounced reflexes.

0


source


How about if you consider using the push model to iterate in parallel and raise an event so that the consumer will simply process each item as it appears?

0


source







All Articles