Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Incorrect Elapsed Time

Status
Not open for further replies.

vbcnew55

Technical User
Mar 2, 2008
7
CA
I'm trying to measure how long it takes to run code. So I added the stopwatch object in the code, started it, and debug printed the elapsed time. The elapsed time is incorrect.

Can somebody tell me what I did wrong here?

Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim x As Long, sw As New System.Diagnostics.Stopwatch

        sw.Start()

        For x = 1 To 5000
            Debug.Print(Now & vbTab & x & vbTab & sw.Elapsed.TotalSeconds.ToString("0.000"))
        Next

        Debug.Print("elapsed time " & sw.Elapsed.TotalSeconds.ToString("0.000"))

    End Sub

During each pass in the For-Loop, the date stamp prints the wrong time...seems to be lagging. What is causing this?
 
How do you mean 'Prints the wrong time'?

As you are doing 5001 Debug.Print() statements the code execution speed will be slowed dramatically and the output time may appear to be out of sync with the actual time elapsed to run the code (this is basically because of the extra load String I/O puts on the machine) as the code is outputting the string 'after the fact' so to speak.

How about changing your code to show a more realistic output (that will be pretty close to the time taken by the app to process the loop)?
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim x As Long, sw As New Stopwatch
        Dim sb As New StringBuilder(5000)

        sw.Start()

        For x = 1 To 5000
            'Debug.Print(Now & vbTab & x & vbTab & sw.Elapsed.TotalSeconds.ToString("0.000"))
            sb.AppendLine(Now & vbTab & x & vbTab & sw.Elapsed.TotalSeconds.ToString("0.000"))
        Next

        Debug.Print(sb.ToString())

        Debug.Print("elapsed time " & sw.Elapsed.TotalSeconds.ToString("0.000"))

    End Sub
All of the above is under the assumption that I've understood your problem correctly. If not please post back and clarify your query.

Hope this helps

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
Thanks for the reply.

I'll try it out, and post a response tonight.
 
Hi,

Just wanted to get back to you; yep that was what I was looking for...going to read up some more on the string builder class.

thanks again.
 
Glad I could help [smile]

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top