If ((ProcOne = True) And (ProcTwo = True)) Then...If ((PA = 1) And (PB = 1)) 26.07 s If (PA = 1) Then If (PB = 1) 13.58 sWhere PA and PB simply return zero.
If (PA = 1) Then If (PB = 1) Then End If End IfWhich takes more space, but certainly aids show the flow. You could nest these several levels deep - but don't forget to put the clause most likely to equate to false on the outside.
I saw a program that performed the anti-alias by averaging the pixels itself, instead of using the API. It was not particularly quick, but it worked.
Now we've pointed out what a pain in the butt GDI can be, it is time to concentrate on purely VB code optimisations.
What follows may surprise you.
MyStr$ = "" 16.14 s MyStr$ = vbNullString 3.94 sPlease be aware that while the above will work in VB programs, there is an important difference in that the vbNullString value is a null pointer while the "" value is a pointer is an empty string. This may be of critical importance if/when calling DLLs (i.e. Windows API functions). For normal internal use, however, vbNullString is where it's at.
If (MyStr$ = "") 7.20 s If (MyStr$ = vbNullString) 6.59 s If (Len(MyStr$) = 0) 4.86 s If (LenB(MyStr$) = 0) 5.21 s
MyVal = Asc(MyStr$) 9.99 s MyVal = AscW(MyStr$) 4.34 s MyStr$ = Chr$(120) 21.45 s MyStr$ = ChrW$(120) 17.99 s
process = LCase(Trim(inline))
process$ = LCase$(Trim$(inline))
process = LCase(Trim(inline))
pr = LCase(Trim(**)) 141.66 s pr$ = LCase$(Trim$(**)) 105.80 s
MyLong = Val("1234")
MyLong = CLng("1234")
MyLong = Val("1234") 75.39 s MyLong = CLng("1234") 30.25 s
Dim MyStr$ As String, MyVal As Long
Dim MyStr, MyVal
DefInt A-Z
"MyInt = 12345 3.39 s MyLong = 12345 3.42 s MyLong = 12345& 3.05 s MyVar = 12345 4.31 sRemember these timings are for twenty million iterations. As you can see, there is not much in it. Long with long is best, as it is the native word size of the x86 processor. However int with int is not much worse, followed by long from an int (remember to type appropriately).
For MyLoop = 1& To 511&
VB timings log file - created 2007/03/15 at 22:01 ================================================= Times are for repeating the process 20,000,000 times. Assigning to an Int from an Int 3.385 Assigning to a Long from an Int 3.419 Assigning to a Long from a Long 3.052 Assigning to a Variant from an Int 4.305 If loop with two functions together 26.074 If loop with two functions separate 13.578 Blanking a string with "" 16.137 Blanking a string with vbNullString 3.939 String blank test with "" 7.197 String blank test with vbNullString 6.591 String blank test with Len() = 0 4.861 String blank test with LenB() = 0 5.211 Converting string to int using Asc 9.998 Converting string to int using AscW 4.335 Converting int to string using Chr 25.423 Converting int to string using Chr$ 21.445 Converting int to string using ChrW$ 17.987 MyStr = LCase(Trim(" HeyRick ") 141.658 MyStr$ = LCase$(Trim$(" HeyRick ") 105.8 Convert string num to long with Val 75.392 Convert string num to long with CLng 30.245 Tests completed after 530.504 seconds.
' SpeedTest v0.01 ' by Rick Murray 15th March 2007 ' http://www.heyrick.co.uk/software/vbopti.html ' Option Explicit ' A multimedia function to return the number of milliseconds since the last reset. Declare Function timeGetTime Lib "WinMM" () As Long ' Variables. :-) Public FP As Integer ' File Pointer Public LN As String ' Logfile Name Public TL As Long ' Timing Loop value Public TS As String ' Temp String Public ST As Long ' Start Time Public ET As Long ' End Time Public RT As Single ' Resultant Time Public BT As Long ' Begin Time Public Const LC As Long = 20000000 ' Loop Count ' Here be dragons... Public Sub Main() Dim MyInt As Integer, MyLong As Long, MyVar As Variant, MyStr As String On Error Resume Next ' Confirm If (MsgBox("This test program will take a while (perhaps several minutes)." & vbCrLf & _ "It is recommended that you close all other applications and let this " & _ "program run" & vbCrLf & "without disturbances, and that you run it TWICE " & _ "and take an average of the results." & vbCrLf & vbCrLf & _ "Continue?", vbQuestion + vbYesNo + vbDefaultButton1) = vbNo) Then End ' Load and open the status window Load frmProgress frmProgress.Show DoEvents ' Screen hourglass Screen.MousePointer = vbHourglass ' The log file name is... LN = App.Path & "\VB speed log.txt" ' Open log file FP = FreeFile Open LN For Binary Access Write As FP ' Write a header TS = "VB timings log file - created " & Format(Now, "Short Date") & _ " at " & Format(Now, "Short Time") & vbCrLf Put #FP, , TS TS = String$((Len(TS) - 2), "=") & vbCrLf & vbCrLf Put #FP, , TS TS = "Times are for repeating the process " & Format(LC, "##,###,###") & _ " times." & vbCrLf & vbCrLf Put #FP, , TS ' Record start time BT = timeGetTime ' ======== ' PART ONE - variable timings ' ======== ST = timeGetTime For TL = 1& To LC MyInt = 12345 ' <-- we assume this will assign 12345 as an Int, NOT via a Variant! Next ET = timeGetTime RT = ((CSng(ET) - CSng(ST)) / 1000#) TS = "Assigning to an Int from an Int " & CStr(RT) & vbCrLf Put #FP, , TS ST = timeGetTime For TL = 1& To LC MyLong = 12345 Next ET = timeGetTime RT = ((CSng(ET) - CSng(ST)) / 1000#) TS = "Assigning to a Long from an Int " & CStr(RT) & vbCrLf Put #FP, , TS ST = timeGetTime For TL = 1& To LC MyLong = 12345& Next ET = timeGetTime RT = ((CSng(ET) - CSng(ST)) / 1000#) TS = "Assigning to a Long from a Long " & CStr(RT) & vbCrLf Put #FP, , TS ST = timeGetTime For TL = 1& To LC MyVar = 12345 Next ET = timeGetTime RT = ((CSng(ET) - CSng(ST)) / 1000#) TS = "Assigning to a Variant from an Int " & CStr(RT) & vbCrLf & vbCrLf Put #FP, , TS frmProgress.SetProgress 1, 7 ' ======== ' PART TWO - IF timings ' ======== ST = timeGetTime For TL = 1& To LC If ((PA = 1) And (PB = 1)) Then End If Next ET = timeGetTime RT = ((CSng(ET) - CSng(ST)) / 1000#) TS = "If loop with two functions together " & CStr(RT) & vbCrLf Put #FP, , TS ST = timeGetTime For TL = 1& To LC If (PA = 1) Then If (PB = 1) Then End If End If Next ET = timeGetTime RT = ((CSng(ET) - CSng(ST)) / 1000#) TS = "If loop with two functions separate " & CStr(RT) & vbCrLf & vbCrLf Put #FP, , TS frmProgress.SetProgress 2, 7 ' ========== ' PART THREE - Blank string timings ' ========== ST = timeGetTime For TL = 1& To LC MyStr$ = "" Next ET = timeGetTime RT = ((CSng(ET) - CSng(ST)) / 1000#) TS = "Blanking a string with " & Chr$(34) & Chr$(34) & " " & CStr(RT) & vbCrLf Put #FP, , TS ST = timeGetTime For TL = 1& To LC MyStr$ = vbNullString Next ET = timeGetTime RT = ((CSng(ET) - CSng(ST)) / 1000#) TS = "Blanking a string with vbNullString " & CStr(RT) & vbCrLf & vbCrLf Put #FP, , TS frmProgress.SetProgress 3, 7 ' ========= ' PART FOUR - Testing is a string is blank timings ' ========= MyStr$ = "heyrick" ST = timeGetTime For TL = 1& To LC If (MyStr$ = "") Then End If Next ET = timeGetTime RT = ((CSng(ET) - CSng(ST)) / 1000#) TS = "String blank test with " & ChrW$(34) & ChrW$(34) & " " & CStr(RT) & vbCrLf Put #FP, , TS ST = timeGetTime For TL = 1& To LC If (MyStr$ = vbNullString) Then End If Next ET = timeGetTime RT = ((CSng(ET) - CSng(ST)) / 1000#) TS = "String blank test with vbNullString " & CStr(RT) & vbCrLf Put #FP, , TS ST = timeGetTime For TL = 1& To LC If (Len(MyStr$) = 0) Then End If Next ET = timeGetTime RT = ((CSng(ET) - CSng(ST)) / 1000#) TS = "String blank test with Len() = 0 " & CStr(RT) & vbCrLf Put #FP, , TS ST = timeGetTime For TL = 1& To LC If (LenB(MyStr$) = 0) Then End If Next ET = timeGetTime RT = ((CSng(ET) - CSng(ST)) / 1000#) TS = "String blank test with LenB() = 0 " & CStr(RT) & vbCrLf & vbCrLf Put #FP, , TS frmProgress.SetProgress 4, 7 ' ========= ' PART FIVE - To and from ASCII timings ' ========= MyStr$ = "x" ST = timeGetTime For TL = 1& To LC MyInt = Asc(MyStr$) Next ET = timeGetTime RT = ((CSng(ET) - CSng(ST)) / 1000#) TS = "Converting string to int using Asc " & CStr(RT) & vbCrLf Put #FP, , TS ST = timeGetTime For TL = 1& To LC MyInt = AscW(MyStr$) Next ET = timeGetTime RT = ((CSng(ET) - CSng(ST)) / 1000#) TS = "Converting string to int using AscW " & CStr(RT) & vbCrLf Put #FP, , TS ST = timeGetTime For TL = 1& To LC MyStr$ = Chr(MyInt) Next ET = timeGetTime RT = ((CSng(ET) - CSng(ST)) / 1000#) TS = "Converting int to string using Chr " & CStr(RT) & vbCrLf Put #FP, , TS ST = timeGetTime For TL = 1& To LC MyStr$ = Chr$(MyInt) Next ET = timeGetTime RT = ((CSng(ET) - CSng(ST)) / 1000#) TS = "Converting int to string using Chr$ " & CStr(RT) & vbCrLf Put #FP, , TS ST = timeGetTime For TL = 1& To LC MyStr$ = ChrW$(MyInt) Next ET = timeGetTime RT = ((CSng(ET) - CSng(ST)) / 1000#) TS = "Converting int to string using ChrW$ " & CStr(RT) & vbCrLf & vbCrLf Put #FP, , TS frmProgress.SetProgress 5, 7 ' ======== ' PART SIX - With and without $ timings ' ======== ST = timeGetTime For TL = 1& To LC MyStr = LCase(Trim(" HeyRick ")) Next ET = timeGetTime RT = ((CSng(ET) - CSng(ST)) / 1000#) TS = "MyStr = LCase(Trim(" & ChrW$(34) & " HeyRick " & ChrW$(34) & ") " & _ CStr(RT) & vbCrLf Put #FP, , TS ST = timeGetTime For TL = 1& To LC MyStr$ = LCase$(Trim$(" HeyRick ")) Next ET = timeGetTime RT = ((CSng(ET) - CSng(ST)) / 1000#) TS = "MyStr$ = LCase$(Trim$(" & ChrW$(34) & " HeyRick " & ChrW$(34) & ") " & _ CStr(RT) & vbCrLf & vbCrLf Put #FP, , TS frmProgress.SetProgress 6, 7 ' ========== ' PART SEVEN - Number to string cast timings ' ========== ST = timeGetTime For TL = 1& To LC MyLong = Val("1234") Next ET = timeGetTime RT = ((CSng(ET) - CSng(ST)) / 1000#) TS = "Convert string num to long with Val " & CStr(RT) & vbCrLf Put #FP, , TS ST = timeGetTime For TL = 1& To LC MyLong = CLng("1234") Next ET = timeGetTime RT = ((CSng(ET) - CSng(ST)) / 1000#) TS = "Convert string num to long with CLng " & CStr(RT) & vbCrLf & vbCrLf Put #FP, , TS frmProgress.SetProgress 7, 7 ' Done, close file and open it... ET = timeGetTime RT = ((CSng(ET) - CSng(BT)) / 1000#) TS = "Tests completed after " & CStr(RT) & " seconds." & vbCrLf Put #FP, , TS Close #FP Shell "Notepad " & ChrW$(34) & LN & ChrW$(34), vbNormalFocus ' Final tidyups Screen.MousePointer = vbDefault frmProgress.Hide Unload frmProgress End End Sub Private Function PA() As Integer PA = 0 End Function Private Function PB() As Integer PB = 0 End Function |