note 90499 deleted from function.strtok by danbrown

From: Date: Sat, 25 Apr 2009 14:50:47 +0000
Subject: note 90499 deleted from function.strtok by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-153718@lists.php.net to get a copy of this message
Note Submitter: Bob ---- I was interested in the performance of various tokenizing algorithms, in particular splitting a string by a list of tokens. I've compared 3 methods, including a custom function based on strtok(). My aim was to split a MySQL type definition into its various parts, i.e. split "int(10) unsigned zerofill" to Array( 1 => "int", 2 => "10", 3 => " unsigned zerofill" ) This is the code I ran: <?php function getmicrotime() { list($usec, $sec) = explode(" ",microtime()); return ((float)$usec + (float)$sec); } function toksplit($string, $tokens) { $val = array(); $v = strtok($string, $tokens); do $val[] = $v; while($v = strtok($tokens)); return $val; } echo "<pre>"; $string = "int(10) unsigned zerofill"; $ntrials = 100000; for($i = 0, $t = getmicrotime(); $i < $ntrials; ++$i) $toksplit_result = toksplit($string, '()'); $time = getmicrotime()-$t; $avg = $time/$ntrials; echo "toksplit(): $avg\n"; print_r($toksplit_result); for($i = 0, $t = getmicrotime(); $i < $ntrials; ++$i) $split_result = split('[()]', $string); $time = getmicrotime()-$t; $avg = $time/$ntrials; echo "split(): $avg\n"; print_r($split_result); for($i = 0, $t = getmicrotime(); $i < $ntrials; ++$i) $preg_split_result = preg_split('/[()]/', $string); $time = getmicrotime()-$t; $avg = $time/$ntrials; echo "preg_split(): $avg\n"; print_r($preg_split_result); echo "</pre>"; ?> These were the results: toksplit(): 1.94901299477E-5 Array ( [0] => int [1] => 100 [2] => unsigned zerofill ) split(): 1.50095391273E-5 Array ( [0] => int [1] => 100 [2] => unsigned zerofill ) preg_split(): 1.82743000984E-5 Array ( [0] => int [1] => 100 [2] => unsigned zerofill ) Interestingly, split() was the fastest. I also modified my test to also strip whitespace from each token, changing the patterns to "\s*[()]\s*" and adding a trim() call to the toksplit() function. The results went from fastest to slowest: preg_split(), split(), toksplit(). I would infer from this that split() is the fastest if you want to simply split by a list of delimiters. However, if you want to split by a more complicated pattern, preg_split() is the best. strtok() is clearly slower. NOTE: if you want to split by a single delimiter (not a list or pattern), use explode().

« previous php.notes (#153718) next »