-
Apr 19th, 2015, 02:03 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Visual Basic - Code Syntax Speed
Hello
i am still new to visual basic i coded in RealBasic for years i wrote a code i was trying to convert to from RealBasic to VisualBasic its a speed benchmark test the speed of a piece code used i used to use this code below in RB to profile my code any ideas how i can it can be converted to VisualBasic? Thanks
Code:
Private Sub Command1_Click()
Dim i As Integer
Dim max As Integer
max = 2000000
Dim dump As Integer
Dim x As Integer
Dim start1, start2, start3 As Double
Dim end1, end2, end3 As Double
start1 = Microseconds
For i = 0 To max
x = x Mod 3
If x = 0 Then dump = 0
If x = 1 Then dump = 1
If x = 2 Then dump = 2
Next
end1 = Microseconds
start2 = Microseconds
For i = 0 To max
x = x Mod 3
If x = 0 Then
dump = 0
ElseIf x = 1 Then
dump = 1
ElseIf x = 2 Then
dump = 2
End If
Next i
end2 = Microseconds
start3 = Microseconds
For i = 0 To max
x = x Mod 3
Select Case x
Case 0
dump = 0
Case 1
dump = 1
Case 2
dump = 2
End Select
Next
end3 = Microseconds
MsgBox = Format(end1 - start1, "0") + EndOfLine + _
Format(end2 - start2, "0") + EndOfLine + _
Format(end3 - start3, "0")
End Sub
-
Apr 19th, 2015, 02:23 PM
#2
Re: Visual Basic - Code Syntax Speed
Yea... or you could use GetTickCount API.
"Retrieves the number of milliseconds that have elapsed since the system was started"
So retrieve it once before you execute your code, do your code, and retrieve it again, now deduct from one another.
Code:
Private Declare Function GetTickCount Lib "kernel32" () As Long
Code:
Dim lngTimeTook as Long
lngTimeTook = GetTickCount()
'do some code
lngTimeTook = GetTickCount() - lngTimeTook
Msgbox ("This took: " & lngTimeTook & " milisec to do")
Last edited by stum; Apr 19th, 2015 at 02:26 PM.
-
Apr 19th, 2015, 02:44 PM
#3
Re: Visual Basic - Code Syntax Speed
-
Apr 19th, 2015, 02:48 PM
#4
Re: Visual Basic - Code Syntax Speed
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Apr 19th, 2015, 04:02 PM
#5
Re: Visual Basic - Code Syntax Speed
while your at, test this... avoiding MOD
Code:
x = 0
For i = 0 To max
If x = 0 Then
dump = 0
x = 1
ElseIf x = 1 Then
dump = 1
x = 2
Else
dump = 2
x = 0
End If
Next
-
Apr 21st, 2015, 04:36 AM
#6
Thread Starter
Addicted Member
Re: Visual Basic - Code Syntax Speed
The GetTickCount seems to work pretty good for timing to see what VB code run time is almost same as RB elseif was the fastest!
ElseIf - Fastest - 46 - 47
Select Case - Second Fastest -62 -63
If - ReallySlow- 141- 156
If Else - Same As If - 156- 172
Code:
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Sub Command1_Click()
On Error Resume Next
Dim start1, start2, start3, start4 As Long
Dim end1, end2, end3, end4 As Long
Dim x, i, Max, dump As Integer
Max = 2000000
start1 = GetTickCount()
For i = 0 To Max
If x = 0 Then dump = 0
If x = 1 Then dump = 1
If x = 2 Then dump = 2
Next
end1 = GetTickCount()
start2 = GetTickCount()
For i = 0 To Max
If x = 0 Then
dump = 0
ElseIf x = 1 Then
dump = 1
ElseIf x = 2 Then
dump = 2
End If
Next i
end2 = GetTickCount()
start3 = GetTickCount()
For i = 0 To Max
Select Case x
Case 0
dump = 0
Case 1
dump = 1
Case 2
dump = 2
End Select
Next
end3 = GetTickCount()
start4 = GetTickCount()
For i = 0 To Max
If x = 0 Then dump = 0 Else dump = 0
If x = 1 Then dump = 1 Else dump = 1
If x = 2 Then dump = 2 Else dump = 2
Next
end4 = GetTickCount()
MsgBox "If " + CStr(start1 - end1) & vbCrLf & _
"ElseIf " + CStr(start2 - end2) & vbCrLf & _
"Select Case " + CStr(start3 - end3) & vbCrLf & _
"If Else " + CStr(start4 - end4)
End Sub
Last edited by sinner0636; Apr 21st, 2015 at 05:04 AM.
-
Apr 21st, 2015, 06:28 AM
#7
Re: Visual Basic - Code Syntax Speed
In VB6 you need to dim all variables one at the time
In this situation start1, start2 and start3 are Variants
Code:
Dim start1, start2, start3, start4 As Long
Dim end1, end2, end3, end4 As Long
Dim x, i, Max, dump As Integer
This is what you want:
Code:
Dim start1 As Long, start2 As Long, start3 As Long, start4 As Long
Dim end1 As Long, end2 As Long, end3 As Long, end4 As Long
Dim x As Integer, i As Integer, Max As Integer, dump As Integer
-
Apr 21st, 2015, 09:58 AM
#8
Re: Visual Basic - Code Syntax Speed
sinner0636, please attach exe-files which were compiled by vb6 and rb. Only you should set the all optimization options in the compiler properties. Let's make up the similary code samples, and i will testing and i will show assembler listing. Ok? Sorry my bad English.
-
Apr 21st, 2015, 11:29 AM
#9
Re: Visual Basic - Code Syntax Speed
Originally Posted by The trick
sinner0636, please attach exe-files which were compiled by vb6 and rb. Only you should set the all optimization options in the compiler properties. Let's make up the similary code samples, and i will testing and i will show assembler listing. Ok? Sorry my bad English.
Exe files are not allowed as attachments
-
Apr 21st, 2015, 01:00 PM
#10
Re: Visual Basic - Code Syntax Speed
Originally Posted by DataMiser
Exe files are not allowed as attachments
I hasn't PB for compile project, therefore i cannot compare performance. He can just change a (the) extension of executable files.
-
Apr 21st, 2015, 01:02 PM
#11
Re: Visual Basic - Code Syntax Speed
You should read the forum rules, Binaries are not allowed, Source code only.
-
Apr 21st, 2015, 01:04 PM
#12
Re: Visual Basic - Code Syntax Speed
Originally Posted by DataMiser
You should read the forum rules, Binaries are not allowed, Source code only.
Ok, I understood.
-
Apr 21st, 2015, 02:34 PM
#13
Thread Starter
Addicted Member
Re: Visual Basic - Code Syntax Speed
thanks for the the tip stum and Arnoutdv
-
Apr 21st, 2015, 03:12 PM
#14
Re: Visual Basic - Code Syntax Speed
Originally Posted by sinner0636
The GetTickCount seems to work pretty good for timing to see what VB code run time is almost same as RB elseif was the fastest!
As already mentioned by others, for a true comparison on max achievable speed,
you would need to:
- use the VB6-native-Compiler (checking in all the extended Options in the appropriate Dialogue)
- Remove the active Error-Handler from routines which need to be performed at full-speed (testing them in the IDE beforehand)
Then the following is the output from a native compiled Executable:
which had the following Test-Code inside:
Code:
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Sub Form_Click()
Dim i As Long, x As Long, start1 As Long, end1 As Long, Max As Long, dump As Long
Max = 200000000 '<- former Max was 2000000
start1 = GetTickCount()
For i = 0 To Max
x = i Mod 10 'put x into a reasonable range (circulating between 0 to 9)
If x = 0 Then dump = 0
If x = 1 Then dump = 1
If x = 2 Then dump = 2
Next
end1 = GetTickCount()
Print "If comparisons: " & (end1 - start1)
End Sub
Note, that in the above code, the loop-count (compared to your example) was increased by factor 100!
@The trick (Anatoli)
The OP was comparing with RealBasic (RB) not with PowerBasic (PB)
Olaf
-
Apr 21st, 2015, 03:25 PM
#15
Re: [RESOLVED] Visual Basic - Code Syntax Speed
Schmidt, I just wanted to show that the test does not make sense. The compiler optimizes the expression, and if the variable is not used then it is removed. Here is the listing of your code (only cycle):
As can be seen there was only "naked" cycle. Any manipulation with the "x"-variables were excluded.
-
Apr 21st, 2015, 05:59 PM
#16
Re: [RESOLVED] Visual Basic - Code Syntax Speed
Originally Posted by The trick
I just wanted to show that the test does not make sense. The compiler optimizes the expression, and if the variable is not used then it is removed.
Well, wasn't *that* obvious to me, that the VB6-native compiler was *that* clever in this case,
so let's just encapsulate the same code in a small function, and return 'dump' as a result.
Code:
Option Explicit
Private Sub Form_Click()
Dim T As Single: T = Timer
Test
Print Format$((Timer - T) * 1000, "0msec")
End Sub
Private Function Test() As Long
Dim i As Long, x As Long, dump As Long, Max As Long
Max = 20000000 '<- the OPs Max was 2000000
For i = 0 To Max
x = i Mod 10 'put x into a reasonable range (circulating between 0 to 9)
If x = 0 Then dump = 0
If x = 1 Then dump = 1
If x = 2 Then dump = 2
Next
Test = dump
End Function
Now it obviously moves through the instructions - since the performance
is about factor 10 lower now - so I reduced the Max-Loopcount by factor 10.
Here the screen-shot, which shows the same timing-results as measured before (though as said,
with a reduced loopcount of 20000000, which is still factor 10 higher than the one the OP was using)
Think, that this is still a lot faster than what RB achieved on basically the same task
(FWIW... just wanted to point out a rough performance-measure of a native compiled VB6-binary).
Olaf
-
Apr 21st, 2015, 11:37 PM
#17
Re: [RESOLVED] Visual Basic - Code Syntax Speed
Shmidt, ok this test should compile properly. Asm-listing for this example will public later. VB6 has a very good compiler, therefore speed of the compiled codes (mean native,p-code slowly) is not bad.
Added:
This is the assembler listing for your code example:
Last edited by The trick; Apr 22nd, 2015 at 01:41 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|