Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

A Couple Speed Tests


balliztik1
 Share

Recommended Posts

@First:

> Test 1(1) time: 405 milliseconds
> Test 2(1) time: 375 milliseconds
> Test 1(2) time: 405 milliseconds
> Test 2(2) time: 359 milliseconds
> Test 1(3) time: 406 milliseconds
> Test 2(3) time: 374 milliseconds
> Test 1(4) time: 406 milliseconds
> Test 2(4) time: 374 milliseconds
> Test 1(5) time: 406 milliseconds
> Test 2(5) time: 359 milliseconds
>
> Sub Blah()
> Dim a As Long, b As Long, x As Long, e As Byte
> For e = 1 To 5
>     a = GetTickCount
>     scrNumLines.Value = 100
>     For x = 1 To 500000
>         MaxServerLines = scrNumLines.Value
>         lblNumLines.Caption = CStr(scrNumLines.Value)
>     Next
>     b = GetTickCount
>     Call ServerText("Test 1(" & e & ") time: " & (b - a) & " milliseconds")
>    
>     a = GetTickCount
>     scrNumLines.Value = 100
>     For x = 1 To 500000
>         MaxServerLines = scrNumLines.Value
>         lblNumLines.Caption = CStr(MaxServerLines)
>     Next
>     b = GetTickCount
>     Call ServerText("Test 2(" & e & ") time: " & (b - a) & " milliseconds")
>     Next
> End Sub

This sub ran two separate loops, 500000 times, 5 times each. As shownby the results, assigning a property to a variable, then using thevariable is faster than assigning the property twice. From this, youcan conclude that any time you use an object's property more than once,your speed will increase by the usage of a variable.

@First:

> Test 1(1) time: 1762 milliseconds
> Test 2(1) time: 1529 milliseconds
> Test 1(2) time: 1779 milliseconds
> Test 2(2) time: 1529 milliseconds
> Test 1(3) time: 1762 milliseconds
> Test 2(3) time: 1529 milliseconds
> Test 1(4) time: 1763 milliseconds
> Test 2(4) time: 1529 milliseconds
> Test 1(5) time: 1747 milliseconds
> Test 2(5) time: 1529 milliseconds
>
> Sub Blah()
> Dim a As Long, b As Long, x As Long, e As Byte
> For e = 1 To 5
>     a = GetTickCount
>     scrNumLines.Value = 100
>     For x = 1 To 500000
>         MaxServerLines = scrNumLines.Value
>         lblNumLines.Caption = CStr(scrNumLines.Value)
>         lblNumLines.Caption = CStr(scrNumLines.Value)
>         lblNumLines.Caption = CStr(scrNumLines.Value)
>         lblNumLines.Caption = CStr(scrNumLines.Value)
>         lblNumLines.Caption = CStr(scrNumLines.Value)
>     Next
>     b = GetTickCount
>     Call ServerText("Test 1(" & e & ") time: " & (b - a) & " milliseconds")
>  
>     a = GetTickCount
>     scrNumLines.Value = 100
>     For x = 1 To 500000
>         MaxServerLines = scrNumLines.Value
>         lblNumLines.Caption = CStr(MaxServerLines)
>         lblNumLines.Caption = CStr(MaxServerLines)
>         lblNumLines.Caption = CStr(MaxServerLines)
>         lblNumLines.Caption = CStr(MaxServerLines)
>         lblNumLines.Caption = CStr(MaxServerLines)
>     Next
>     b = GetTickCount
>     Call ServerText("Test 2(" & e & ") time: " & (b - a) & " milliseconds")
> Next
> End Sub

This test was identical to the first, save for the number of properties/variables used. When increased from 1 usage to 5, the speed gap widens considerably. The same will likely be observed with even larger numbers.

@First:

> Test 1(1) time: 3447 milliseconds
> Test 2(1) time: 3011 milliseconds
> Test 1(2) time: 3448 milliseconds
> Test 2(2) time: 3011 milliseconds
> Test 1(3) time: 3619 milliseconds
> Test 2(3) time: 3323 milliseconds
> Test 1(4) time: 3588 milliseconds
> Test 2(4) time: 3026 milliseconds
> Test 1(5) time: 3463 milliseconds
> Test 2(5) time: 3074 milliseconds

As suspected, the difference widens even more with 10 usages per loop.
Link to comment
Share on other sites

@Second:

> Test 1(1) time: 733 milliseconds
> Test 2(1) time: 749 milliseconds
> Test 1(2) time: 733 milliseconds
> Test 2(2) time: 734 milliseconds
> Test 1(3) time: 733 milliseconds
> Test 2(3) time: 733 milliseconds
> Test 1(4) time: 733 milliseconds
> Test 2(4) time: 718 milliseconds
> Test 1(5) time: 733 milliseconds
> Test 2(5) time: 733 milliseconds
> Test 1(6) time: 718 milliseconds
> Test 2(6) time: 733 milliseconds
> Test 1(7) time: 733 milliseconds
> Test 2(7) time: 718 milliseconds
> Test 1(8) time: 733 milliseconds
> Test 2(8) time: 733 milliseconds
> Test 1(9) time: 718 milliseconds
> Test 2(9) time: 733 milliseconds
> Test 1(10) time: 749 milliseconds
> Test 2(10) time: 733 milliseconds
>
> Sub Blah()
> Dim a As Long, b As Long, x As Long, e As Byte
> For e = 1 To 10
>     a = GetTickCount
>     scrNumLines.Value = 100
>     For x = 1 To 1000000
>         MaxServerLines = scrNumLines.Value
>         lblNumLines.Caption = MaxServerLines
>     Next
>     b = GetTickCount
>     Call ServerText("Test 1(" & e & ") time: " & (b - a) & " milliseconds")
>  
>     a = GetTickCount
>     scrNumLines.Value = 100
>     For x = 1 To 1000000
>         MaxServerLines = scrNumLines.Value
>         lblNumLines.Caption = CStr(MaxServerLines)
>     Next
>     b = GetTickCount
>     Call ServerText("Test 2(" & e & ") time: " & (b - a) & " milliseconds")
> Next
> End Sub

This sub tested converting an integer with CStr vs. leaving it as a Integer at run-time. As shown by the results, conversions fluctuated. There is no clear advantage to either method. A second test was run, in a slightly different fashion.

@Second:

> Test 1(1) time: 733 milliseconds
> Test 1(2) time: 733 milliseconds
> Test 1(3) time: 733 milliseconds
> Test 1(4) time: 733 milliseconds
> Test 1(5) time: 718 milliseconds
> Test 1(6) time: 733 milliseconds
> Test 1(7) time: 733 milliseconds
> Test 1(8) time: 734 milliseconds
> Test 1(9) time: 717 milliseconds
> Test 1(10) time: 733 milliseconds
> Test 2(1) time: 734 milliseconds
> Test 2(2) time: 733 milliseconds
> Test 2(3) time: 733 milliseconds
> Test 2(4) time: 733 milliseconds
> Test 2(5) time: 733 milliseconds
> Test 2(6) time: 734 milliseconds
> Test 2(7) time: 733 milliseconds
> Test 2(8) time: 733 milliseconds
> Test 2(9) time: 733 milliseconds
> Test 2(10) time: 733 milliseconds

This time, test 1 was run 10 times, then test 2 was run 10 times, instead of alternating. These results show that choosing not to convert a value is more prone to fluctuation, but is generally faster, for the given value. This may change depending on the value used. More tests will confirm or deny a trend.
Link to comment
Share on other sites

@Third:

> Test 1(1): 2000 ms
> Test 2(1): 8579 ms
> Test 1(2): 1953 ms
> Test 2(2): 8562 ms
> Test 1(3): 1985 ms
> Test 2(3): 8562 ms
> Test 1(4): 1969 ms
> Test 2(4): 8578 ms
> Test 1(5): 2000 ms
> Test 2(5): 8625 ms
>
> Sub Blah()
>     Dim numbers(10000) As Long, x As Long, a As Long, b As Long, e As Byte, y As Integer, total As Long, q As Variant
>     For x = 0 To 10000
>         numbers(x) = x
>     Next
>    
>     For e = 1 To 5
>         a = GetTickCount
>         For x = 1 To 5000
>         total = 0
>             For y = 0 To UBound(numbers)
>                 total = total + numbers(y)
>             Next
>         Next
>         b = GetTickCount
>         Text1.Text = Text1.Text + "Test 1(" & e & "): " & (b - a) & " ms" & vbNewLine
>        
>         total = 0
>         a = GetTickCount
>         For x = 1 To 5000
>         total = 0
>             For Each q In numbers
>                 total = total + q
>             Next
>         Next
>         b = GetTickCount
>         Text1.Text = Text1.Text + "Test 2(" & e & "): " & (b - a) & " ms" & vbNewLine
>     Next
> End Sub

This test compared using For Each to For with a control variable. While For Each doesn't have to address each element of an array separately (as they are stored in the control), which makes it faster, it is required to use Variant type variables. Variants are often regarded as the worst data type there is, as they are too large for their purposes. As shown here, 5000 loops through adding 10000 elements was drastically reduced with a simple For loop.

@Third:

> Test 1(1): 1500 ms
> Test 2(1): 2172 ms
> Test 1(2): 1547 ms
> Test 2(2): 2156 ms
> Test 1(3): 1485 ms
> Test 2(3): 2156 ms
> Test 1(4): 1500 ms
> Test 2(4): 2188 ms
> Test 1(5): 1484 ms
> Test 2(5): 2156 ms

By drastically reducing the number of elements in the array, the difference is cut down. Shown here is 255 elements, or the max capacity of a byte. Still, though, the difference is very large.

@Third:

> Test 1(1): 94 ms
> Test 2(1): 156 ms
> Test 1(2): 94 ms
> Test 2(2): 156 ms
> Test 1(3): 109 ms
> Test 2(3): 156 ms
> Test 1(4): 94 ms
> Test 2(4): 156 ms
> Test 1(5): 110 ms
> Test 2(5): 156 ms

Shown here, using just a single element array, the difference is still huge.
Link to comment
Share on other sites

It's merely data. I explain a situation and provide two sets of code, then show speed differences, and a little commentary. Optimizations like these are minor, but add up. Mostly I'm doing this just out of curiosity, but since I have the data and everyone can benefit, I might as well share it. Plus, I'm including all the code so anyone can test this. It's in lab report format. Detached, but precise, pointing out trends and providing enough info for it to be recreated, and allows people to draw their own conclusions.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...