accurate measurement and reduce jitter

41 views Asked by At

I am trying to understand how to measure a block of code to maximize performance.

On doing some tests I found there to be quite a few outliers that have large differences from the average. The following piece of code, measures a block of code that takes about 400ns to run and counts the outliers. On a windows 10 laptop, I get about 99.77% of the results under 600ns. But how do I reduce the jitter? (similar results using .net 6 and .net 7. Intel CPU. I have disabled hyperthreading)

        public static void Test()
        {
            int counter;
            int c = -1;
            int[] outliers = new int[20];
            for (counter = 0; counter < 10000000; ++counter)
            {
                long startTime = Stopwatch.GetTimestamp();
                TestFunction(Environment.TickCount, 1000);
                long endTime = Stopwatch.GetTimestamp() - startTime;
                endTime *= 100;
                switch (endTime)
                {
                    case > 2000:
                        outliers[0]++;
                        break;
                    case > 1900:
                        outliers[1]++;
                        break;
                    case > 1800:
                        outliers[2]++;
                        break;
                    case > 1700:
                        outliers[3]++;
                        break;
                    case > 1600:
                        outliers[4]++;
                        break;
                    case > 1500:
                        outliers[5]++;
                        break;
                    case > 1400:
                        outliers[6]++;
                        break;
                    case > 1300:
                        outliers[7]++;
                        break;
                    case > 1200:
                        outliers[8]++;
                        break;
                    case > 1100:
                        outliers[9]++;
                        break;
                    case > 1000:
                        outliers[10]++;
                        break;
                    case > 900:
                        outliers[11]++;
                        break;
                    case > 800:
                        outliers[12]++;
                        break;
                    case > 700:
                        outliers[13]++;
                        break;
                    case > 600:
                        outliers[14]++;
                        break;
                    case > 500:
                        outliers[15]++;
                        break;
                    case > 400:
                        outliers[16]++;
                        break;
                    case > 300:
                        outliers[17]++;
                        break;
                    case > 200:
                        outliers[18]++;
                        break;
                    case > 100:
                        outliers[19]++;
                        break;

                }
            }
 
            for (int i = 0; i < outliers.Length; ++i)
            {
                Console.WriteLine($"For timing > {2000 - i * 100} ns count:{outliers[i]}");
            }
        }
        public static long TestFunction(long seed, int count)
        {
            long result = seed;
            for (int i = 0; i < count; ++i)
            {
                result ^= i ^ seed; // Some useless bit operations
            }
            return result;
        }

Results:

For timing > 2000 ns count:8563
For timing > 1900 ns count:200
For timing > 1800 ns count:276
For timing > 1700 ns count:522
For timing > 1600 ns count:433
For timing > 1500 ns count:538
For timing > 1400 ns count:1742
For timing > 1300 ns count:1110
For timing > 1200 ns count:4458
For timing > 1100 ns count:4853
For timing > 1000 ns count:19
For timing > 900 ns count:16
For timing > 800 ns count:13
For timing > 700 ns count:31
For timing > 600 ns count:354
For timing > 500 ns count:2395207
For timing > 400 ns count:7581665
For timing > 300 ns count:0
For timing > 200 ns count:0
For timing > 100 ns count:0
0

There are 0 answers