R
R
RoxxelRoxx2020-10-26 13:58:48
C++ / C#
RoxxelRoxx, 2020-10-26 13:58:48

How to count the number of loop iterations in 1 second?

Greetings, a rather trivial task has appeared, but for half an hour I have been puzzling over its solution.
I need to count how many times the loop goes through in 1 second, in other words, the number of iterations per second, how can this be done?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
T
twobomb, 2020-10-26
@RoxxelRoxx

Stopwatch stopWatch = new Stopwatch();
        stopWatch.Start();
    int iterationCounter = 0;
    while(true){			
       //todo
      iterationCounter++;
      if(stopWatch.ElapsedMilliseconds >= 1000){
        Console.WriteLine("iterations "+iterationCounter);
        break;
      }
    }
        stopWatch.Stop();

PS Well, this is not ideal for 1 second, since one second can expire at the time your code is processed, then if you do not need to count such an iteration, subtract 1 from the counter if ElapsedMilliseconds > 1000

P
Peter, 2020-10-26
@petermzg

var sw = new Stopwatch();
sw.Start();
<iterations>
sw.Stop();
mssec = sw.ElapsedMilliseconds;

T
Timohius, 2020-10-26
@TIMOHIUS

Run the cycle for 1000 times (for example), measure the time (N).
Translate the time into seconds (if measured in milliseconds) and 1000\N = number of iterations per second.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question