Elapsed Time for a process

snufse

Newcomer
Joined
Jan 30, 2004
Messages
10
Location
west palm beach
I am using the following code to get the elapsed time for a process. The difference is calculated and added to a list box. On first pass the difference in time is calculated and on all subsequent passes elapsed time shows as zero?

Here is the code:

Do

Dim FromTime As DateTime = DateTime.Now

... Here goes the code wich is a call to a stored procedure ....

Dim ToTime As DateTime = DateTime.Now

Dim TimeDifference As TimeSpan = ToTime.Subtract(FromTime)

Dim DifferenceInMilliseconds As Double = TimeDifference.TotalMilliseconds
ListBox1.Items.Add("Processing Time: " & DifferenceInMilliseconds.ToString & " ms")

Loop
 
The behavior you are seeing (greater than zero on the first pass, zero on subsequent passes) is likely due to temporal caching in the loop. The first time through, the information needs to be loaded in memory, but because your are looping, the data stays in memory because it is used often over a short period of time.

Also, perhaps the stopwatch class would be slightly more accurate? It could just be the same, but it seems to be built for this type of activity. If you are using .Net 2.0 this might be something to try to see if you can squeeze a little more precision out of your timing.
 
Back
Top