Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your issue is your test. When you are benchmarking code there are a couple of guiding principals you should always follow:</p> <ol> <li>Processor Affinity: Use only a single processor, usually not #1.</li> <li>Warmup: Always perform the test a small number of times up front.</li> <li>Duration: Make sure your test duration is at least 500ms.</li> <li>Average: Average together multiple runs to remove anomalies.</li> <li>Cleanup: Force the GC to collect allocated objects between tests.</li> <li>Cooldown: Allow the process to sleep for a short-period of time.</li> </ol> <p>So using these guidelines and rewriting your tests I get the following results:</p> <p><strong>Run 1</strong></p> <pre> Enter test number (1|2): 1 ListSum averages 776 ListSum averages 753 ArraySum averages 1102 ListSum averages 753 Press any key to continue . . . </pre> <p><strong>Run 2</strong></p> <pre> Enter test number (1|2): 2 ArraySum averages 1155 ArraySum averages 1102 ListSum averages 753 ArraySum averages 1067 Press any key to continue . . . </pre> <p>So here is the final test code used:</p> <pre><code>static void Main(string[] args) { //We just need a single-thread for this test. Process.GetCurrentProcess().ProcessorAffinity = new IntPtr(2); System.Threading.Thread.BeginThreadAffinity(); Console.Write("Enter test number (1|2): "); string input = Console.ReadLine(); //perform the action just a few times to jit the code. ListArrayLoop warmup = new ListArrayLoop(10, 10); Console.WriteLine("Performing warmup..."); Test(warmup.ListSum); Test(warmup.ArraySum); Console.WriteLine("Warmup complete..."); Console.WriteLine(); ListArrayLoop test = new ListArrayLoop(10000, 10000); if (input == "1") { Test(test.ListSum); Test(test.ListSum); Test(test.ArraySum); Test(test.ListSum); } else { Test(test.ArraySum); Test(test.ArraySum); Test(test.ListSum); Test(test.ArraySum); } } private static void Test(Action test) { long totalElapsed = 0; for (int counter = 10; counter &gt; 0; counter--) { try { var sw = Stopwatch.StartNew(); test(); totalElapsed += sw.ElapsedMilliseconds; } finally { } GC.Collect(0, GCCollectionMode.Forced); GC.WaitForPendingFinalizers(); //cooldown for (int i = 0; i &lt; 100; i++) System.Threading.Thread.Sleep(0); } Console.WriteLine("{0} averages {1}", test.Method.Name, totalElapsed / 10); } </code></pre> <p><em>Note:</em> Some people may debate about the usefulness of the cool-down; However, everyone agrees that even if it's not helpful, it is not harmful. I find that on <em>some</em> tests it can yield a more reliable result; however, in the example above I doubt it makes any difference.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload