note 59435 added to function.explode

From: Date: Wed, 07 Dec 2005 00:37:06 +0000
Subject: note 59435 added to function.explode
Groups: php.notes 
Request: Send a blank email to php-notes+get-99658@lists.php.net to get a copy of this message
The explodei function gave me an idea. I did some modifications to it, and appear to have made it a bit faster. My time-tests aren't scientific either, I just put it in a loop and run it a couple thousand times (more or less, depending on how much data is being processed.) In the original explodei posted, I had to comment out the "if(function_exists("str_ireplace"))" section, as my PHP version is >5; and this is a function being done for <5. :) <?php function explodei($ASeperator, $ASource) { $ASeperator = strtolower($ASeperator); //also create a case insensitive source in memory $x_insensitive = strtolower($ASource); //get result based off insensitive matches. $x_data = explode($ASeperator, $x_insensitive); //iterate each item and transfer over the fields based on length $x_position = 0; //don't keep calling strlen() in a loop on a consistent source.. icky. $x_sep_len = strlen($ASeperator); //iterate all elements obtained via explode() assigned to $x_data foreach ($x_data as $myElement) { //How long is this particular element? $x_element_length = strlen($myElement); //create new result element, we have the position and length, grab it from //the ORIGINAL source, so we don't pass along non-original info. $retval[] = substr($ASource, $x_position, $x_element_length); //increase position by length of seperator, and length of this element. $x_position += $x_element_length + $x_sep_len; } //return our results return $retval; } ?> The results! In a PHP <5 version: Testing source: <?php function microtime_float() { list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); } //Be careful! This increases exponentially. :) for ($x=0,$x_source="TEST|";$x<16;$x++) $x_source .= $x_source; echo "\$x_source length: " . strlen($x_source) . "\n"; $x_micro_start = microtime_float(); //increase or decrease $x<10 to whatever. //Don't do many iterations on a high $x value above. for ($x = 0; $x<2; $x++) $myResult = explodei('|', $x_source); $x_micro_stop = microtime_float(); $x_micro_time = $x_micro_stop - $x_micro_start; echo "Time: {$x_micro_time} seconds.\n"; ?> The original explodei: akede:/home/akede # php -f ip2loc.php $x_source length: 327680 Time: 151.550167084 seconds. My version: akede:/home/akede # php -f ip2loc.php $x_source length: 327680 Time: 0.943643808365 seconds. The reason it's faster? Because it relies on built-in functions more than the original. Explode does the "hard work" and finds all the positions we're going to need. In the original, it uses the scripting environment to iterate the entire source string, .. hmm. Actually, if I understand that right, it looks like it actually is going off a 1-character seperator in the original source. I guess that works. Explode(), AFAIK supports strings for seperators. Anyway, it works. Write something faster, play with it, use it, whatever. It's all fun-- have fun! ---- Manual Page -- http://www.php.net/manual/en/function.explode.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+59435 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59435&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59435&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59435&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59435&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59435&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59435&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59435&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+59435&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#99658) next »