|
|
Title | Tip: Use LCase$ and UCase$ instead of the CharLower and CharUpper API functions |
Description | This example demonstrates that the LCase$ and UCase$ functions are faster than the CharLower and CharUpper API functions in Visual Basic 6, at least some of the time. |
Keywords | CharLower, CharUpper, case, LCase, UCase |
Categories | Miscellany, Strings |
|
|
This example converts a string into lower case using Visual Basic's LCase and LCase$ functions, and the CharLower API function. I recently saw a Microsoft performance article that said CharLower should be faster than LCase$.
In this test, LCase$ was the fastest. LCase was a bit slower (perhaps 10%) because it returns a Variant rather than a String and Visual Basic needs to convert it into a String. CharLower was the slowest. In some tests, it took about 20% longer than LCase.
It just goes to show, you can't believe everything you read.
|
|
Private Sub cmdCharLower_Click()
...
num_trials = CLng(txtNumTrials.Text)
start_time = Timer
For i = 1 To num_trials
txt = CharLower(TEST_TEXT)
Next i
stop_time = Timer
...
End Sub
Private Sub cmdLCase_Click()
...
start_time = Timer
For i = 1 To num_trials
txt = LCase$(TEST_TEXT)
Next i
stop_time = Timer
...
End Sub
Private Sub cmdLCase1_Click()
...
start_time = Timer
For i = 1 To num_trials
txt = LCase(TEST_TEXT)
Next i
stop_time = Timer
...
End Sub
|
|
Adelle explains:
The reason why VB6's LCase$ function is faster than using the CharLower api
function has to do with the assumptions that VB6 makes with regard to the
rest of the outside world.
Internally, all strings in VB6 are stored as unicode. For Win9x
compatibility, strings passed to external api functions are converted to
multi-byte.
This conversion not only takes time, but produces a less culturally aware
result.
|
|
|
|
|
|