TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack (1) Thread Tools Search this Thread Display Modes
Old 12-12-2007, 07:49 AM   1 links from elsewhere to this Post. Click to view. #1 (permalink)
Jay
The Contributor
Good Samaritan 
 
Join Date: Dec 2007
Posts: 53
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
Old 12-12-2007, 09:46 AM   #2 (permalink)
The Acquainted
 
sjaq's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 104
Thanks: 9
sjaq is on a distinguished road
Default

There is another method wich is about 20 times faster on my computer:
PHP Code:
if(!empty($string{32})) {
        
// string is 33 + chars

sjaq is offline  
Reply With Quote
Old 12-12-2007, 12:00 PM   #3 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 1,587
Thanks: 72
Wildhoney is on a distinguished road
Default

Fascinating finds Although there's the distinct possibility that Jay's PC is just slower than yours, sjag. Be good to test them side-by-side on the same PC.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-12-2007, 12:04 PM   #4 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 438
Thanks: 22
Karl is on a distinguished road
Default

Nice fine indeed. However, the question is, are you going to sacrifice the "readability" of your conditionals for the sake of a few milliseconds? Personally, I wouldn't
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Karl is offline  
Reply With Quote
Old 12-12-2007, 12:20 PM   #5 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 445
Thanks: 49
ReSpawN is on a distinguished road
Default

Quote:
Originally Posted by Karl View Post
Nice fine indeed. However, the question is, are you going to sacrifice the "readability" of your conditionals for the sake of a few milliseconds? Personally, I wouldn't
I'm with Karl on this one. The strlen function typs really easy, can be remembered by even the beginners and ofcourse, be used for a LOT of other diffirent things. I use $string[$i] for displaying the last character in a sentence. For example, $i = strlen($string); echo $string[$i];.

Very nice find, but I wouldn't use it either. Maybe there might be some who find it more useful than me but personally, I do not see the gain.

Non the less, good job! And try what Wildhoney said!

Mark
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 12-12-2007, 12:29 PM   #6 (permalink)
Jay
The Contributor
Good Samaritan 
 
Join Date: Dec 2007
Posts: 53
Thanks: 5
Jay is on a distinguished road
Default

The results on my benchmark output relatively the same speed..

Also, why use { } and not [ ]? It's the same thing, [ ] looks neater

The reason I want to use it.. because it's less typing , and I can read it about, if not, the same.
Jay is offline  
Reply With Quote
Old 12-12-2007, 01:29 PM   #7 (permalink)
bdm
The Acquainted
Good Samaritan 
 
Join Date: Nov 2007
Posts: 127
Thanks: 14
bdm is on a distinguished road
Default

I also prefer using {} as opposed to [].

It's a matter of preference. Just make sure you stay consistent in your code and all will be fine.
bdm is offline  
Reply With Quote
Old 12-12-2007, 01:48 PM   #8 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 1,587
Thanks: 72
Wildhoney is on a distinguished road
Default

Aren't they removing the curly braces in PHP 5.3/6?
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-12-2007, 02:16 PM   #9 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 438
Thanks: 22
Karl is on a distinguished road
Default

If they are, I won't miss em :)
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Karl is offline  
Reply With Quote
Old 12-12-2007, 04:51 PM   #10 (permalink)
The Acquainted
 
sjaq's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 104
Thanks: 9
sjaq is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
Fascinating finds Although there's the distinct possibility that Jay's PC is just slower than yours, sjag. Be good to test them side-by-side on the same PC.
I tested them on the same computer with this code:
PHP Code:
<?php 

    
require '../speed.php';
    
    
$string 'Sjaakie moet poeeepeeneeeeeeeen!';
    
    
Speed::start('strlen');
    
    for(
$i=0,$max=20000;$i $max;$i++) {
        if(
strlen($string) > 10) {
            
$henk 'skaal';
        }
    }
    
    
Speed::stop('strlen');
    
    
Speed::start('{strlen}');
    
    for(
$i=0,$max=20000;$i $max;$i++) {
        if(!empty(
$string{10})) {
            
$henk 'skaal';
        }
    }
    
    
Speed::stop('{strlen}');
    
    
Speed::compare('strlen''{strlen}');

?>
(speed.php contains a simple speed testing class)
sjaq is offline  
Reply With Quote
Old 12-29-2007, 04:16 AM   #11 (permalink)
The Wanderer
 
deflated's Avatar
 
Join Date: Dec 2007
Location: 127.0.0.1
Posts: 18
Thanks: 7
deflated is on a distinguished road
Default

isset() is a language construct and not a function just like echo, require, include, etc. Therefore it's much faster than a 'real' function like strlen().

Quote:
Originally Posted by bdm View Post
I also prefer using {} as opposed to [].
I use {} to access a character in a string and [] to access an element in an array. It's easier for others to understand the code when you differentiate between these two 'constructs'.

Quote:
Originally Posted by Wildhoney View Post
Aren't they removing the curly braces in PHP 5.3/6?
I haven't heard about that and I don't hope so.

Last edited by deflated : 12-29-2007 at 02:35 PM.
deflated is offline  
Reply With Quote
Old 12-29-2007, 01:37 PM   #12 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 1,587
Thanks: 72
Wildhoney is on a distinguished road
Default

I always found it a bit odd accessing characters in a string as you would items in an array. I've always been tempted to use str_split to make it make sense.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-29-2007, 02:34 PM   #13 (permalink)
The Wanderer
 
deflated's Avatar
 
Join Date: Dec 2007
Location: 127.0.0.1
Posts: 18
Thanks: 7
deflated is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
I always found it a bit odd accessing characters in a string as you would items in an array. I've always been tempted to use str_split to make it make sense.
Wildhoney, I don't see any advantages in using str_split() instead of accessing directly the characters. Actually, there are no differences when using {} or [] except of it's much faster because it's a language construct and str_split() is a function. sort(), array_combine(), and all other functions explicitly expect an array as an argument and wouldn't work in most of the cases with strings properly. So why do prefer to have a 'real' array even though it's slower and does only make sense if you need to perform actions on that array. Perhaps there are not any big differences (Of course that depends on the length of the string!) but imagine you're using it str_split() in a loop with one million iterations. I guess the 'little' difference might be significant.
deflated is offline  
Reply With Quote
Old 12-29-2007, 02:54 PM   #14 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 1,587
Thanks: 72
Wildhoney is on a distinguished road
Default

Yeah. I don't, but I am always tempted to because accessing characters in a string as you would items in an array seems absurd to me.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-29-2007, 07:25 PM   #15 (permalink)
The Frequenter
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 359
Thanks: 3
xenon is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
Yeah. I don't, but I am always tempted to because accessing characters in a string as you would items in an array seems absurd to me.
But then, if you think about it, it makes perfect sense. Think about it: what is an array: an enumeration of various data types. What is a string: an enumeration of characters. So, since they are both enumerations, it makes sense to be able to access strings as arrays. Also, they are not going to remove the curly brace syntax. If my memory is good, they said using curly braces when accessing string elements instead of square brackets is faster for the simple fact that:
- square brackets are made for array data types, and:
- curly braces were later optimized for string data types (in php 4.x I believe)
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
The Following User Says Thank You to xenon For This Useful Post:
deflated (12-29-2007)
Old 12-30-2007, 10:58 AM   #16 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 713
Thanks: 2
Salathe is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
Aren't they removing the curly braces in PHP 5.3/6?
[Characters within strings] may also be accessed using braces like $str{42} for the same purpose. However, using square array-brackets is preferred because the {braces} style is deprecated as of PHP 6.
__________________
Salathe is online now  
Reply With Quote
The Following 2 Users Say Thank You to Salathe For This Useful Post:
sjaq (01-03-2008), Wildhoney (12-30-2007)
Old 12-30-2007, 01:47 PM   #17 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 1,587
Thanks: 72
Wildhoney is on a distinguished road
Default

I see, I see. I knew I'd heard that somewhere. Thanks Salathe. I suppose it does make sense, it's just to me I don't think of a string as an enumeration, but rather an inseparable sequence. Perhaps I should begin altering my thinking regarding that.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Reply


LinkBacks (?)
LinkBack to this Thread: http://www.talkphp.com/tips-tricks/1716-quicker-string-verification.html
Posted By For Type Date
TalkPHP - Powered by vBulletin This thread Refback 12-29-2007 10:17 PM

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 10:18 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0