Skip to content

Instantly share code, notes, and snippets.

@booyaa
Last active December 18, 2015 09:49
Show Gist options
  • Select an option

  • Save booyaa/5764242 to your computer and use it in GitHub Desktop.

Select an option

Save booyaa/5764242 to your computer and use it in GitHub Desktop.
Benchmarking in C#
// Source: http://www.blackwasp.co.uk/SpeedTestWithStopwatch_2.aspx
// Use it to compare things like da.Fills vs iterating over an OleDbDataReader.
using System;
using System.Diagnostics;
namespace StopWatchExample
{
class Example
{
static void Main(string[] args)
{
Stopwatch timer = new Stopwatch();
long total = 0;
timer.Start(); // Start the timer
for (int i=1;i<=1000000;i++)
{
total += i;
}
timer.Stop(); // Stop the timer
decimal micro = (decimal)timer.Elapsed.Ticks / 10M; // 10,000 microseconds in 1 millisecond
Console.WriteLine("Execution time was {0:F1} microseconds.", micro);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment