Solved

VB.NET 2005 StopWatch - interference by MessageBox?

Posted on 2006-11-03
6
327 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 62

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 62

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 62

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 62

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now