C# tutorials
18. Simple benchmark program

Note: While reading this page, bear in mind that I'm no computer expert and that the text below may be partly inaccurate. If you find errors or have proposals for improvements, please send me a message and help make this a better page for the benefit of future visitors. To the left, there are links to more C# tutorials.

To complete this tutorial, follow these instructions:

1. Open Visual C# 2010 Express.

2. Click on New Project in the File menu.

3. Choose Windows Forms Application if that option isn't already chosen, change the name if you like and click on OK.

4. Point at Toolbox in the left margin and click on the Auto Hide icon at the top of the window (to keep the window to stay open and easier to work with).

5. Drag a Button to Form 1.

6. Drag a Rich Textbox to Form 1. Resize the textbox if you like.

7. Double-click on the button. The file Form1.cs will open with the caret ready to write code that will run when button 1 is clicked.

8. Write or paste this code where the caret is:

int repeat = 10;
List<double> result = new List<double>();
string resultAll = string.Empty;
for (int i = 0; i < repeat; i++)
  Stopwatch sw = new Stopwatch();
  string str = "a b c d e f g h i j k l m n o p q r s t u v w x y z";
  str = str.Replace(" ", "");
  double time = sw.Elapsed.TotalMilliseconds;
double resultAverage = result.Sum() / (repeat - 1);
foreach (double item in result)
  resultAll += item.ToString() + " ms\n";
richTextBox1.Text = "Average:\n" + resultAverage + " ms\n\nAll:\n" + resultAll;

9. Write or paste this code right below using System.Windows.Forms;:

using System.Diagnostics;

10. Press F5 to start debugging the program.

11. Click on the button. A text similar to the one right below should appear in the textbox.

0,00376666666666667 ms

0,0037 ms
0,0034 ms
0,003 ms
0,0034 ms
0,0034 ms
0,0034 ms
0,0034 ms
0,0034 ms
0,0034 ms
0,0034 ms

Comments on the first code snippet

Line 1: Number of times to repeat the code you want to benchmark. You can change 10 to 100 or 1000 or whatever you like.

Line 4-13: Repeat the code in line 8-9 ten times and add the time for each repetition to the List result.

Line 6: Create a new, empty Stopwatch.

Line 7: Start the Stopwatch.

Line 8-9: The code to benchmark. Change these lines to whatever you like.

Line 10: Stop the Stopwatch.

Line 11-12: Get the elapsed time (in milliseconds with four decimals) and add it to the List result.

Line 14: Remove the first element in the List result. Because the first repetion seems to take significantly longer time to execute than the other repetions, I think it's a good idea to remove it before the average time is calculated.

Line 15: Get the average time by getting the sum of all values in the List result and dividing it with the int repeat - 1 (to account for the removed element).

Line 16-19: Convert the List result to the string resultAll

Line 20: Set the text in richTextBox1 to resultAverage and resultAll.

Comment on the second code snippet

A Stopwatch needs the namespace System.Diagnostics to work.

Would you like to comment on this page or some other page? Send an email to mats.kristiansson.skovde@gmail.com or a letter to Mats Kristiansson, Timmervägen 3A, 541 64 Skövde, Sweden with the title of the page you want to comment on, your comment and your name or a pseudonym.