0

I'm trying to run a test in a console app where I control the count of loops per second and getting some weird behavior. Trying to find out if it's my machine or what...

I wrote the below in a console app...

//1 seconds in nanoseconds = 1000000000 / 100 for the Timespan = 10000000
long secondIn100NanoUnits = 10000000;

//for each test
List<int> loopsPerSecond = new();

//different tests
loopsPerSecond.Add(1);
loopsPerSecond.Add(100);
loopsPerSecond.Add(1000);
loopsPerSecond.Add(10000);
loopsPerSecond.Add(100000);
loopsPerSecond.Add(1000000);
loopsPerSecond.Add(10000000);

int expectedTestLengthInSeconds = 5;

TimeSpan delay;

//run tests
//running foreward or backward has same behavior
for (int i = 0; i < loopsPerSecond.Count; i++)
//for (int i = loopsPerSecond.Count - 1; i >= 0; i--)
{
    int thisloopsPerSecond = loopsPerSecond[i];

    long loopDelayTimespan = secondIn100NanoUnits / thisloopsPerSecond;
    delay = new(loopDelayTimespan);
    int thisloops = thisloopsPerSecond * expectedTestLengthInSeconds;

    Console.WriteLine($"Test {i + 1} -  {thisloops} loop(s) at {thisloopsPerSecond} loops per second, with {loopDelayTimespan} 100 nanoSec unit loop delay");

    DateTime dateTime = DateTime.Now;
    for (int j = 0; j < thisloops; j++)
    {
        Thread.Sleep(delay);
    }
    TimeSpan time = DateTime.Now.Subtract(dateTime);

    double avgMsPerTick = time.TotalMilliseconds / thisloops;
    double actualloopsPerSecond = 1000 / avgMsPerTick;
    Console.WriteLine($"{time.TotalMilliseconds} total Ms, {Math.Round(avgMsPerTick, 5)} avg ms per tick, roughly {Math.Round(actualloopsPerSecond, 2)} actual loops per second");
    Console.WriteLine(Environment.NewLine);
}

Console.WriteLine("Done");
Console.ReadLine();

and got the output below...

I expected each test to run at about 5 seconds. Test 1 ran exactly as expected, but tests 2 and 3 ran way too long and roughly the same speeds. Tests 4+ ran much faster than expected.

I get the same results when I run the outer loop backwards too.

Anybody have any idea why these loops are not running close to their expected times? Why 500 and 5000 loops are consistently slow and 50000+ loops are fast?

Is there something underlying that I'm missing?

Is my test flawed somehow?

Test 1 - 5 loop(s) at 1 loops per second, with 10000000 100 nanoSec unit loop delay 5042.8732 total Ms, 1008.57464 avg ms per tick, roughly 0.99 actual loops per second

Test 2 - 500 loop(s) at 100 loops per second, with 100000 100 nanoSec unit loop delay 7861.2627 total Ms, 15.72253 avg ms per tick, roughly 63.6 actual loops per second

Test 3 - 5000 loop(s) at 1000 loops per second, with 10000 100 nanoSec unit loop delay 78466.8776 total Ms, 15.69338 avg ms per tick, roughly 63.72 actual loops per second

Test 4 - 50000 loop(s) at 10000 loops per second, with 1000 100 nanoSec unit loop delay 53.8743 total Ms, 0.00108 avg ms per tick, roughly 928086.3 actual loops per second

Test 5 - 500000 loop(s) at 100000 loops per second, with 100 100 nanoSec unit loop delay 461.6911 total Ms, 0.00092 avg ms per tick, roughly 1082975.18 actual loops per second

Test 6 - 5000000 loop(s) at 1000000 loops per second, with 10 100 nanoSec unit loop delay 4211.5956 total Ms, 0.00084 avg ms per tick, roughly 1187198.51 actual loops per second

Test 7 - 50000000 loop(s) at 10000000 loops per second, with 1 100 nanoSec unit loop delay 37734.9539 total Ms, 0.00075 avg ms per tick, roughly 1325031.43 actual loops per second

Done

xSquared
  • 53
  • 2
  • 6
  • You really should have printed `delay`... but anyway duplicate explains what you see for non-zero sleep intervals (which is what you are asking about). There should be separate one for zero-time sleeps... – Alexei Levenkov Apr 22 '22 at 00:50
  • Thanks Alexei, I'll check it out. – xSquared Apr 22 '22 at 09:40

0 Answers0