Solved

VB.NET 2005 StopWatch - interference by MessageBox?

Posted on 2006-11-03
6
339 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

832 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