Sancler
asked on
VB.NET 2005 StopWatch - interference by MessageBox?
For the background to this, look here
https://www.experts-exchange.com/questions/22046611/what's-quicker-equal-to-or-not-equal-to.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.To String & " ms" & vbCrLf & _
"String code run time " & sw2.ElapsedMilliseconds.To String & " 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.To String & " ms" & vbCrLf & _
"String code run time " & sw2.ElapsedMilliseconds.To String & " 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
https://www.experts-exchange.com/questions/22046611/what's-quicker-equal-to-or-not-equal-to.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.To
"String code run time " & sw2.ElapsedMilliseconds.To
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.To
"String code run time " & sw2.ElapsedMilliseconds.To
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
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
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
ASKER
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.To String & " 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.To String & " 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.
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.To
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.To
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
Well thank you very much. But I am going to continue looking at this issue.
Have a great day. ;=)
Fernando
Have a great day. ;=)
Fernando
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