Performance Tip: Upper Case vs Lower Case
Many people think that converting a given string to upper case and converting to lower case will perform the same. While they probably do use a very similar algorithm, the fact the most text is lower case makes converting to lower cause a more efficient option for most strings. The only time they will perform the same is if half the text is upper case, and half is lower case. Sometimes you have a choice on which case you can convert to, for instance:
<cfquery> SELECT firstname FROM users WHERE UPPER( firstname ) = <cfqueryparam value="#UCase(url.firstname)#" cfsqltype="cf_sqlvarchar"> </cfquery>
In this example (searching for a firstname) it doesn't really make sense to convert to upper case, assuming the first name is stored something like Pete
, and the user input was probably either Pete
or pete
. We can save some resources by converting to lower case instead.
To understand the differences lets take a look at how one might implement an algorithm to convert a string to upper case.
loop over each character { if (character code is in the range of a lower case character) { character code = character code + offset; } }
So if a character is uppercase (not in the range of the lower case character codes) then all that happens is a simple numerical compersion, and then move on to the next character. If the character is lower case then we still do the numerical compersion, and also perform an addition (which includes two reads, and a mathematical operation), and write a value to memory.
So next time you have a choice to either convert a string to upper case or lower case stop for a second and think about the string. In most cases it will make sense to convert to lower case. In some cases such as with acronyms it makes more sense to use upper case.
The performance gains from this probably isn't something worth checking all your existing code for, but is something to keep in mind for the future.
Tweet
Comments
Post a Comment
Recent Entries
- Facebook API Now Requires Review for user_friends Permission
- Docker Container exited with code 137
- Why is my cron.daily script not running?
- Announcing FuseGuard Version 3
- CFSummit 2017
- Java Unlimited Strength Crypto Policy for Java 9 or 1.8.0_151
- Java 9 Security Enhancements
- Upcoming CFML Conferences in April 2017