First, there is a class of development tools called
Profilers. A good Profiler runs a software product under control and helps to find out performance bottlenecks very rapidly. The developer uses this information to improve performance of small fraction of performance critical code and runs it under the Profiler again.
You can try to perform your search to find appropriate Profiler. At this moment, I cannot give certain recommendations for a good .NET Profiler.
If you need to measure performance of the fragments of code only occasionally, you can create a simple home-baked
ad-hoc "Profiler" using the class
System.Diagnostics.Stopwatch
:
System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
stopWatch.Start();
stopWatch.Stop();
System.TimeSpan duration = stopWatch.Elapsed;
double elapsedTimeMs = duration.TotalMilliseconds;
The class
Stopwatch
has much better accuracy compared to timer classes or
System.DateTime
class; this is the best class to measure performance.
—SA