Why does the second for loop always execute faster than the first one?

前端 未结 8 1271
日久生厌
日久生厌 2021-01-05 11:14

I was trying to figure out if a for loop was faster than a foreach loop and was using the System.Diagnostics classes to time the task. While running the test I noticed that

8条回答
  •  [愿得一人]
    2021-01-05 11:53

    1. I would not use DateTime to measure performance - try the Stopwatch class.
    2. Measuring with only 4 passes is never going to give you a good result. Better use > 100.000 passes (you can use an outer loop). Don't do Console.WriteLine in your loop.
    3. Even better: use a profiler (like Redgate ANTS or maybe NProf)

提交回复
热议问题