explode vs preg_split

Submitted by tomek on Sun, 05/08/2011 - 14:03

f2 is slower than f1 by 68.73%

Full source code as executed.
define('LOOP',100000);
function f1() {
  $a = "comma,delimited,string";
  for($i=0; $i<LOOP; ++$i) {
    explode(',',$a);
    explode(',',$a);
    explode(',',$a);
    explode(',',$a);
    explode(',',$a);
  }
}
 
function f2() {
  $a = "comma,delimited,string";
  for($i=0; $i<LOOP; ++$i) {
    preg_split('/,/',$a);
    preg_split('/,/',$a);
    preg_split('/,/',$a);
    preg_split('/,/',$a);
    preg_split('/,/',$a);
  }
}
 
$start = microtime(true);
f1();
$stop = microtime(true);
$time1 = $stop - $start;
 
$start = microtime(true);
f2();
$stop = microtime(true);
$time2 = $stop - $start;
 
echo $time1 . "\t";
echo $time2 . "\n";
Test results
comparisontime1time2php_version
61.95%146175123672935.3.5-1ubuntu7.2
38.6%159140122056575.3.3-7
71.99%251131043191625.3.5-1ubuntu7.2
51.23%140032921177695.3.3-7+squeeze1
68.48%174744294414php-5.3.6-phpbrew_compiled
66.35%177126294656php-5.3.7-phpbrew_compiled
67.14%180127301058php-5.3.8-phpbrew_compiled
66.01%178373296113php-5.3.9-phpbrew_compiled
70.24%172394293492php-5.3.10-phpbrew_compiled
71.39%171543293999php-5.3.11-phpbrew_compiled
71.09%171703293759php-5.3.12-phpbrew_compiled
70.86%172228294271php-5.3.13-phpbrew_compiled
72.73%168619291262php-5.3.14-phpbrew_compiled
71.75%176185302591php-5.3.15-phpbrew_compiled
70.04%172697293652php-5.3.16-phpbrew_compiled
58.28%189717300282php-5.3.17-phpbrew_compiled
74.18%172413300306php-5.3.18-phpbrew_compiled
66.82%173899290107php-5.3.19-phpbrew_compiled
79.58%165226296716php-5.4.0-phpbrew_compiled
75.45%167847294480php-5.4.1-phpbrew_compiled
62.73%183161298067php-5.4.2-phpbrew_compiled
76.02%171266301467php-5.4.3-phpbrew_compiled
78.29%164323292977php-5.4.4-phpbrew_compiled
78.26%164657293514php-5.4.5-phpbrew_compiled
68.93%173989293923php-5.4.6-phpbrew_compiled
72.89%170035293977php-5.4.7-phpbrew_compiled
74.97%166283290953php-5.4.8-phpbrew_compiled
68.32%167196281417php-5.4.9-phpbrew_compiled
50%
updown
50%