Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleTip: Use LCase$ and UCase$ instead of the CharLower and CharUpper API functions
DescriptionThis 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.
KeywordsCharLower, CharUpper, case, LCase, UCase
CategoriesMiscellany, 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.

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated