View Single Post
Old 12-12-2007, 07:49 AM   #1 (permalink)
Jay
The Contributor
Good Samaritan 
 
Join Date: Dec 2007
Posts: 60
Thanks: 5
Jay is on a distinguished road
Default Quicker String Verification

I thought of a way to validate string length earlier today, and when I went to go benchmark it, it proved to be faster/more efficient than using strlen.

My method:
PHP Code:
// We want strings less than or equal to 32 characters
if( isset( $string[32] ) )
{
    
// Failed! This string is 33 characters! (arrays are 0 indexed, so 32 characters is index 31, and 33 is index 32!)

After ten-thousand iterations, it proved to only be 0.001500s, where as:

PHP Code:
if( strlen$string ) > 32 )
{
    
// Failed! This string is 33+ characters!

Took about 0.006000s

So as I see it, using my method proves to be very efficient (3x's faster than strlen). Any objections, comments, or complaints before I state this as a standard in my mind?
Jay is offline  
Reply With Quote