 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
Advertisement
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
12-12-2007, 07:49 AM
|
#1 (permalink)
|
|
The Contributor
Join Date: Dec 2007
Posts: 53
Thanks: 5
|
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? 
|
|
|
|
12-12-2007, 09:46 AM
|
#2 (permalink)
|
|
The Acquainted
Join Date: Nov 2007
Location: Netherlands
Posts: 104
Thanks: 9
|
There is another method wich is about 20 times faster on my computer:
PHP Code:
if(!empty($string{32})) { // string is 33 + chars }
|
|
|
|
12-12-2007, 12:00 PM
|
#3 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 1,587
Thanks: 72
|
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.
|
|
|
12-12-2007, 12:04 PM
|
#4 (permalink)
|
|
The Reckoner
Join Date: Sep 2007
Posts: 438
Thanks: 22
|
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.
|
|
|
|
12-12-2007, 12:20 PM
|
#5 (permalink)
|
|
The Frequenter
Join Date: Nov 2007
Location: Netherlands
Posts: 445
Thanks: 49
|
Quote:
Originally Posted by Karl
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"
|
|
|
12-12-2007, 12:29 PM
|
#6 (permalink)
|
|
The Contributor
Join Date: Dec 2007
Posts: 53
Thanks: 5
|
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.
|
|
|
|
12-12-2007, 01:29 PM
|
#7 (permalink)
|
|
The Acquainted
Join Date: Nov 2007
Posts: 127
Thanks: 14
|
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. 
|
|
|
|
12-12-2007, 01:48 PM
|
#8 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 1,587
Thanks: 72
|
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.
|
|
|
12-12-2007, 02:16 PM
|
#9 (permalink)
|
|
The Reckoner
Join Date: Sep 2007
Posts: 438
Thanks: 22
|
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.
|
|
|
|
12-12-2007, 04:51 PM
|
#10 (permalink)
|
|
The Acquainted
Join Date: Nov 2007
Location: Netherlands
Posts: 104
Thanks: 9
|
Quote:
Originally Posted by Wildhoney
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)
|
|
|
|
12-29-2007, 04:16 AM
|
#11 (permalink)
|
|
The Wanderer
Join Date: Dec 2007
Location: 127.0.0.1
Posts: 18
Thanks: 7
|
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
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
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.
|
|
|
|
12-29-2007, 01:37 PM
|
#12 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 1,587
Thanks: 72
|
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.
|
|
|
12-29-2007, 02:34 PM
|
#13 (permalink)
|
|
The Wanderer
Join Date: Dec 2007
Location: 127.0.0.1
Posts: 18
Thanks: 7
|
Quote:
Originally Posted by Wildhoney
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.
|
|
|
|
12-29-2007, 02:54 PM
|
#14 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 1,587
Thanks: 72
|
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.
|
|
|
12-29-2007, 07:25 PM
|
#15 (permalink)
|
|
The Frequenter
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 359
Thanks: 3
|
Quote:
Originally Posted by Wildhoney
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.
|
|
|
|
|
The Following User Says Thank You to xenon For This Useful Post:
|
|
12-30-2007, 10:58 AM
|
#16 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 713
Thanks: 2
|
Quote:
Originally Posted by Wildhoney
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.
__________________
|
|
|
|
|
The Following 2 Users Say Thank You to Salathe For This Useful Post:
|
|
12-30-2007, 01:47 PM
|
#17 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 1,587
Thanks: 72
|
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.
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|