HomeSearch

C# Benchmark

Use Stopwatch to benchmark a method, computing the average number of nanoseconds.
Benchmark. If we have a solid knowledge of how fast code executes, we can develop more efficient programs. This opens new possibilities.StopwatchConvert Nanoseconds
Benchmarking, notes. With benchmarking, we can make smarter decisions. Most of the programs we use every day have been benchmarked many times.
Example. First we see some benchmark loops. This program is what I use for my experiments. Change "_max" depending on the code of each iteration. Start smaller and push the limit up.

Note: Use the two loops here to time code. The two stopwatches are set up to print out the time.

For

Note 2: Run in a new C# console application. Adjust _max higher or lower based on how slow your iterations are.

Console.WriteLine

Note 3: Always run in Release mode, never in Visual Studio—click the .exe yourself. Change the order of the tests.

Benchmarking Console application, 2 loops: C# using System; using System.Diagnostics; using System.Runtime.CompilerServices; class Program { const int _max = 1000000; static void Main() { var s1 = Stopwatch.StartNew(); for (int i = 0; i < _max; i++) { } s1.Stop(); var s2 = Stopwatch.StartNew(); for (int i = 0; i < _max; i++) { } s2.Stop(); Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); Console.Read(); } static int _temp1; static int _temp2; [MethodImpl(MethodImplOptions.NoInlining)] static void Method1() { } [MethodImpl(MethodImplOptions.NoInlining)] static void Method2() { } } Benchmarking Console application, 3 loops: C# using System; using System.Diagnostics; class Program { static void Main() { const int m = 1000000; Stopwatch s1 = Stopwatch.StartNew(); for (int i = 0; i < m; i++) { } s1.Stop(); Stopwatch s2 = Stopwatch.StartNew(); for (int i = 0; i < m; i++) { } s2.Stop(); Stopwatch s3 = Stopwatch.StartNew(); for (int i = 0; i < m; i++) { } s3.Stop(); Console.WriteLine("{0},{1},{2}", s1.ElapsedMilliseconds, s2.ElapsedMilliseconds, s3.ElapsedMilliseconds); Console.Read(); } } Benchmarking aspx file: ASP.NET and C# using System; using System.Diagnostics; using System.Web.UI; public partial class _Default : Page { protected void Page_Load(object sender, EventArgs e) { const int m = 1000000; Stopwatch s1 = Stopwatch.StartNew(); for (int i = 0; i < m; i++) { } s1.Stop(); Stopwatch s2 = Stopwatch.StartNew(); for (int i = 0; i < m; i++) { } s2.Stop(); Response.Write("Loop 1: "); Response.Write(s1.ElapsedMilliseconds); Response.Write("<br/>" + "Loop 2: "); Response.Write(s2.ElapsedMilliseconds); } }
Some problems. The benchmarking code has some problems. The second block sometimes takes less time to execute due to unknown causes.

Tip: Repeat and swap two loops if you are doubtful. The first program shown converts the results to nanoseconds.

Nanoseconds: It is usually best to report results in nanoseconds or microseconds with the number of iterations used to divide the result.

An example optimization. The Dictionary collection in the base class library is a huge optimization. But developers sometimes write code that results in twice as many lookups.TryGetValue
Summary. Benchmarking encourages careful thinking about your code. It saves nanoseconds from your software. It also improves the depth of your understanding.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls