Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...