Solved

VB.NET 2005 StopWatch - interference by MessageBox?

Posted on 2006-11-03
6
343 Views
Last Modified: 2008-02-01
For the background to this, look here

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_22046611.html

Here's a sub that was intended to test alternative coding of IF statements.  The issue now, however, is the behaviour of the StopWatch.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sw1 As New Stopwatch
        Dim sw2 As New Stopwatch
        Dim Maxloops As Integer = 100000000
        Dim var1 As Integer = 100
        Dim Var2 As String = "ABC"
        Dim msg As String

        sw1.Stop()
        sw1.Reset()
        sw1.Start()
        For idx As Integer = 0 To Maxloops
            If var1 = 100 Then
                var1 += 1
            Else
                var1 -= 1
            End If
        Next
        sw1.Stop()

        sw2.Reset()
        sw2.Start()
        For idx As Integer = 0 To Maxloops
            If Var2 = "ABC" Then
                Var2 = "ABCD"
            Else
                Var2 = "ABC"
            End If
        Next
        sw2.Stop()

        msg = "Numeric code run time " & sw1.ElapsedMilliseconds.ToString & " ms" & vbCrLf & _
        "String code run time " & sw2.ElapsedMilliseconds.ToString & " ms"
        MessageBox.Show(msg, "Equals Version")
        Debug.WriteLine("Equals Version")
        Debug.WriteLine(msg)

        sw1.Reset()
        sw1.Start()
        For idx As Integer = 0 To Maxloops
            If var1 <> 100 Then
                var1 -= 1
            Else
                var1 += 1
            End If
        Next
        sw1.Stop()

        sw2.Reset()
        sw2.Start()
        For idx As Integer = 0 To Maxloops
            If Var2 <> "ABC" Then
                Var2 = "ABC"
            Else
                Var2 = "ABCD"
            End If
        Next
        sw2.Stop()

        msg = "Numeric code run time " & sw1.ElapsedMilliseconds.ToString & " ms" & vbCrLf & _
        "String code run time " & sw2.ElapsedMilliseconds.ToString & " ms"
        MessageBox.Show(msg, "Not Equals Version")
        Debug.WriteLine("Not Equals Version")
        Debug.WriteLine(msg)


    End Sub

When the sub was originally run, it produced the following results.

WITH FIRST MESSAGE BOX LEFT IN

Equals Version
Numeric code run time 346 ms
String code run time 1621 ms
Not Equals Version
Numeric code run time 1080 ms
String code run time 1608 ms

Equals Version
Numeric code run time 352 ms
String code run time 1624 ms
Not Equals Version
Numeric code run time 1067 ms
String code run time 1605 ms

Equals Version
Numeric code run time 359 ms
String code run time 1628 ms
Not Equals Version
Numeric code run time 1155 ms
String code run time 1612 ms

Equals Version
Numeric code run time 354 ms
String code run time 1615 ms
Not Equals Version
Numeric code run time 1111 ms
String code run time 1596 ms

Equals Version
Numeric code run time 353 ms
String code run time 1626 ms
Not Equals Version
Numeric code run time 1217 ms
String code run time 1626 ms

The results looked odd.  The major difference between the two versions being tested so far as the Numeric code was concerned was totally counter-intuitive.

So I revised the code in a number of ways, and finally refined the issue to the first occurrence of the line

        MessageBox.Show(msg, "Equals Version")

If I commented that out, the results were in line with expectations.  Here they are.

WITH FIRST MESSAGE BOX COMMENTED OUT

Equals Version
Numeric code run time 354 ms
String code run time 1657 ms
Not Equals Version
Numeric code run time 354 ms
String code run time 1627 ms

Equals Version
Numeric code run time 361 ms
String code run time 1621 ms
Not Equals Version
Numeric code run time 360 ms
String code run time 1606 ms

Equals Version
Numeric code run time 352 ms
String code run time 1635 ms
Not Equals Version
Numeric code run time 359 ms
String code run time 1616 ms

Equals Version
Numeric code run time 357 ms
String code run time 1616 ms
Not Equals Version
Numeric code run time 536 ms
String code run time 1731 ms

Equals Version
Numeric code run time 355 ms
String code run time 1624 ms
Not Equals Version
Numeric code run time 357 ms
String code run time 1654 ms

So, what gives?  First, can anyone else reproduce this effect?  Second, can anyone explain how the message box line - which sits between, on the one hand, the stopping of the stopwatch sw1 and, on the other, the resetting and starting of that stopwatch - can affect the subsequent reading of the stopwatch?

Roger
0
Comment
Question by:Sancler
  • 4
  • 2
6 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17866979
Hi Roger;

I load the above code into a new project and ran it and got the same results as you did. I went back and ran the code from the original question and get the result as originally posted in that question. I am going to try and spend some time looking into this over the weekend.

Fernando
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17877408
Hi Roger;

I looked at the code that the two versions produce, the MSIL code, and both are the same from what I can see except for the one missing the call to the message box and one does not. It must be a bug in Microsoft code unless you came up with something different.

Have a great day;
Fernando

0
 
LVL 34

Author Comment

by:Sancler
ID: 17878050
Fernando

The call to the message box is obviously part of it, but it goes further than that.

Try this

    Private Sub test()

        Dim sw1 As New Stopwatch
        Dim Maxloops As Integer = 100000000
        Dim var1 As Integer = 100
        Dim Var2 As String = "ABC"
        Dim msg As String

        sw1.Start()
        For idx As Integer = 0 To Maxloops
            If var1 = 100 Then
                var1 += 1
            Else
                var1 -= 1
            End If
        Next
        sw1.Stop()

        For idx As Integer = 0 To Maxloops
            If Var2 = "ABC" Then
                Var2 = "ABCD"
            Else
                Var2 = "ABC"
            End If
        Next

        msg = "Numeric code run time " & sw1.ElapsedMilliseconds.ToString & " ms"
        MessageBox.Show(msg, "Equals Version")
        Debug.WriteLine("Equals Version")
        Debug.WriteLine(msg)

        sw1.Reset()
        sw1.Start()
        For idx As Integer = 0 To Maxloops
            If var1 <> 100 Then
                var1 -= 1
            Else
                var1 += 1
            End If
        Next
        sw1.Stop()

        For idx As Integer = 0 To 1
            If Var2 <> "ABC" Then
                Var2 = "ABC"
            Else
                Var2 = "ABCD"
            End If
        Next

        msg = "Numeric code run time " & sw1.ElapsedMilliseconds.ToString & " ms"
        MessageBox.Show(msg, "Not Equals Version")
        Debug.WriteLine("Not Equals Version")
        Debug.WriteLine(msg)
        Debug.WriteLine("====")

    End Sub

It's based on the original code, but (1) with only the times for the Numeric tests being reported and (2) the Maxloops being replaced by 1 for the SECOND run of the Alpha test.

Run it a few times.

Then just make one change.  Replace Maxloops by 1 for the FIRST run of the Alpha test.

Run it a few more times.

Here's my results

Equals Version
Numeric code run time 930 ms
Not Equals Version
Numeric code run time 350 ms
====
Equals Version
Numeric code run time 888 ms
Not Equals Version
Numeric code run time 364 ms
====
Equals Version
Numeric code run time 919 ms
Not Equals Version
Numeric code run time 355 ms
====

CODE CHANGE MADE HERE

Equals Version
Numeric code run time 358 ms
Not Equals Version
Numeric code run time 904 ms
====
Equals Version
Numeric code run time 358 ms
Not Equals Version
Numeric code run time 937 ms
====
Equals Version
Numeric code run time 352 ms
Not Equals Version
Numeric code run time 981 ms
====

My conclusion?  I'm going to be VERY wary of using the Stopwatch for testing purposes ;-)

Roger

PS  I'll leave this open for a while to see if anyone else has any comments although - as it's now a few days old, I'm not hopeful.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 17881751
Hi Roger;

That just does not make any sense to me, what is going on here. Well one thing for sure I will not be using StopWatch until I can find out more on its erratic behavior.

My test results using your code.

Equals Version
Numeric code run time 1059 ms
Not Equals Version
Numeric code run time 536 ms
====
Equals Version
Numeric code run time 1043 ms
Not Equals Version
Numeric code run time 483 ms
====
Equals Version
Numeric code run time 1100 ms
Not Equals Version
Numeric code run time 480 ms
====

CODE CHANGE MADE HERE

Equals Version
Numeric code run time 468 ms
Not Equals Version
Numeric code run time 1053 ms
====
Equals Version
Numeric code run time 465 ms
Not Equals Version
Numeric code run time 1097 ms
====
Equals Version
Numeric code run time 465 ms
Not Equals Version
Numeric code run time 1104 ms
====

Fernando
0
 
LVL 34

Author Comment

by:Sancler
ID: 17906749
Fernando

It doesn't look like there are going to be any other contributions, so the points are yours.  And that should take you over the Genius mark.  Congrats ;-)

Roger
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17907040
Well thank you very much. But I am going to continue looking at this issue.

Have a great day. ;=)

Fernando
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.Net - KeyPress Event 4 38
Modal Popup Extender control 1 38
start a process from a service 3 33
VB.NET String Settings and Temp Folder Question 3 55
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

827 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question