note 57561 deleted from function.explode by bjori

From: Date: Mon, 25 Sep 2006 08:08:15 +0000
Subject: note 57561 deleted from function.explode by bjori
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-117577@lists.php.net to get a copy of this message
Note Submitter: hardy at mapscene dot de ---- //new quick lastword-function demonstration of the fastest way possible! if (!function_exists('microtime_float')) { function microtime_float() { list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); } } function lastwordOLD($string,$seperator=" ") {//last word of an string $array = explode($seperator, $string); $number = count($array); if($number>0) { $number--; return $array[$number]; } return; } //low memory/cpu/parsetime-usage-version of lastword function lastword($string,$seperator=" ") {//last word of an string return substr($string,strrpos($string,$seperator)); } //letz check how good is the new function instead of the old... $time_start = microtime_float(true); print "<br><br>lastwordOLD:"; print "<br>needle included: ".lastwordOLD("hans peter susi luci");//output needle included: luci print "<br>needle not included: ".lastwordOLD("hans;peter;susi;luci");//output needle not included: hans;peter;susi;luci print "<br>runtime-usage x10000: "; $aa=0; while($aa<10000){ $aa++; $request=lastwordOLD("hans peter susi luci"); $request=lastwordOLD("hans;peter;susi;luci"); } $time_end = microtime_float(); $timeOLD = $time_end - $time_start; print $timeOLD; $time_start = microtime_float(true); print "<br><br>lastword(new):"; print "<br>needle included: ".lastword("hans peter susi luci");//output needle included: luci print "<br>needle not included: ".lastword("hans;peter;susi;luci");//output needle not included: hans;peter;susi;luci print "<br>runtime-usage x10000: "; $aa=0; while($aa<10000){ $aa++; $request=lastword("hans peter susi luci"); $request=lastword("hans;peter;susi;luci"); } $time_end = microtime_float(); $time = $time_end - $time_start; print $time; //the result is... print "<br><br>you spare with this script and using new function instead of old: ".($timeOLD-$time)." sec."; //you will see the parsetime of new function procedere will be circa a half of the old! // //result of my webserver , running on an pentium4 2800mhz with suse9 // //lastwordOLD: //needle included: luci //needle not included: hans;peter;susi;luci //runtime-usage x10000: 0.15616607666 // //lastword(new): //needle included: luci //needle not included: hans;peter;susi;luci //runtime-usage x10000: 0.0851180553436 // //you spare with this script and using new function instead of old: 0.0710480213165 sec.

« previous php.notes (#117577) next »