note 90500 added to function.split

From: Date: Sat, 25 Apr 2009 01:58:06 +0000
Subject: note 90500 added to function.split
Groups: php.notes 
Request: Send a blank email to php-notes+get-153704@lists.php.net to get a copy of this message
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(). ---- Server IP: 117.104.162.229 Probable Submitter: 58.96.44.86 (proxied: 58.96.44.86) ---- Manual Page -- http://www.php.net/manual/en/function.split.php Edit -- https://master.php.net/note/edit/90500 Del: integrated -- https://master.php.net/note/delete/90500/integrated Del: useless -- https://master.php.net/note/delete/90500/useless Del: bad code -- https://master.php.net/note/delete/90500/bad+code Del: spam -- https://master.php.net/note/delete/90500/spam Del: non-english -- https://master.php.net/note/delete/90500/non-english Del: in docs -- https://master.php.net/note/delete/90500/in+docs Del: other reasons-- https://master.php.net/note/delete/90500 Reject -- https://master.php.net/note/reject/90500 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#153704) next »