Wednesday, October 14, 2009


This is one of those posts that will be mostly for me, but if it helps anyone else out there, well great. At this point I am re-writing a web page that takes in an Excel spreadsheet and lets the user select which worksheet to process. Well the processing can be done between an old VB6 DLL, which is very slow, and a .NET 2.0 DLL. Now of course the .NET version is much faster, but by how much?

Well in my classic ASP days I would set a Date variable at the beginning and one at the end and find the difference between the two. However, .NET has this nice little feature in the SSystem.Diagnostics assembly that is called "Stopwatch". Once you instantiate the stopwatch you need to call the Start method at the beginning of your code and the Stop method at the end of your code. From there all you need to do is call for the Elapsed property and it will give you back a TimeSpan. So much easier than trying to calculate the difference between two dates.

Dim sw As New System.Diagnostics.Stopwatch
'... Do some long code stuff here ...
Dim ts As TimeSpan = sw.Elapsed
lblErrorMessages.Text = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", sw.Elapsed.Hours, sw.Elapsed.Minutes, sw.Elapsed.Seconds, sw.Elapsed.Milliseconds / 10)