Performance Tip: Upper Case vs Lower Case

coldfusion

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.


3 people found this page useful, what do you think?

 Download FuseGuard WAF for ColdFusion

Trackbacks

Trackback Address: 175/B9FBFE001BA176915CF32D27A45AF7F6

Comments

On 11/06/2004 at 5:44:00 PM UTC Finn wrote:
1
Another thing to note is that case conversions in UTF-8 have the ability to change the stored byte size of the converted string, sometimes resulting in buffer reallocation.

On 08/03/2007 at 10:56:34 AM UTC teddy wrote:
2
Thank for your help

On 08/13/2007 at 4:57:31 PM UTC Dave wrote:
3
If you're talking that level of performance wouldn't

WHERE UPPER( firstname ) = UPPER(<cfqueryparam value="#url.firstname#" cfsqltype="cf_sqlvarchar">)

also be faster? I'm assuming that the DB upper is performed in machine language and CF is interpreted, or at least byte code. I always try and push operations to the DB when possible.

On 02/03/2008 at 8:07:48 PM UTC dalit wrote:
4
thanks for your help

Post a Comment




  



Spell Checker by Foundeo

Recent Entries



foundeo


did you hack my cf?