View Single Post
Old 09-07-2007, 12:18 PM   #8 (permalink)
Salathe
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by Karl View Post
I agree that the RegEx could probably be more readable (if you know a bit of RegEx, that is), however, I was always under the impression that you can't beat regular loops in terms of speed.
PHP Code:
// My version of Wildhoney's getPrefix function
function getPrefixSalathe($szPointer)
{
    if (
preg_match('/^([a-z_]+)[A-Z]?/'$szPointer$aVariable))
        return 
$aVariable[1];
        
    return 
'';

After a quick test here are my results based on calling the function with the single argument "szString".

Calling with "szString" over 100,000 runs:
Wildhoney - 1.3253 seconds
Salathe - 0.9695 seconds

Then again, what real difference is 0.00000356 seconds per function call going to make? :p

As far as being, "under the impression that you can't beat regular loops in terms of speed," that is a good position to hold as a general rule. Sometimes looping through every character (or every item in an array, or whatever) is the clearest, simplest and fastest way to get things done. It is quite easy to get the RegEx parser bogged down - with great power comes great responsibility (to keep things moving slickly).

Finally, it would be slightly more efficient (~0.1141s faster over 100,000 runs) for Wildhoney's function to forgo the str_split and loop through the characters in the string directly if you didn't want to go the RegEx route.
Salathe is offline  
Reply With Quote