preg_split vs explode

Submitted by Ouroboros on Sun, 11/11/2012 - 17:26

f1 is slower than f2 by 45.68%

Full source code as executed.
/* How many times the code under test should run in each function */
define('LOOP',100000);
 
function f1() {
  $string = 'name_25x30.ext';
  for($i=0; $i<LOOP; ++$i) {
 
    $parts = explode('.', $string);
    $name_parts = explode('_', $parts[0]);
    $size_parts = explode('x', $name_parts[1]);
 
  }
}
function f2() {
  $string = 'name_25x30.ext';
  for($i=0; $i<LOOP; ++$i) {
 
    $parts = preg_split('/([0-9a-zA-Z]*)_([0-9]*)x([0-9]*)\.[a-z]*/', $string);
 
  }
}
$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
68.63%7712654573785.3.3-7+squeeze1
49.05%10244968734php-5.3.6-phpbrew_compiled
45.75%10237270238php-5.3.7-phpbrew_compiled
37.91%10077473071php-5.3.8-phpbrew_compiled
40.9%9911170340php-5.3.9-phpbrew_compiled
32.97%10123976134php-5.3.10-phpbrew_compiled
42.88%10013870083php-5.3.11-phpbrew_compiled
42.51%9972569979php-5.3.12-phpbrew_compiled
42.99%10015670042php-5.3.13-phpbrew_compiled
42.97%10039570220php-5.3.14-phpbrew_compiled
48.57%10453770363php-5.3.15-phpbrew_compiled
49.94%10130267560php-5.3.16-phpbrew_compiled
55.33%10727969065php-5.3.17-phpbrew_compiled
32.63%10412978510php-5.3.18-phpbrew_compiled
43.38%10442372830php-5.3.19-phpbrew_compiled
45.5%9401764617php-5.4.0-phpbrew_compiled
45.34%9451065027php-5.4.1-phpbrew_compiled
52%10031865999php-5.4.2-phpbrew_compiled
45.33%9563265805php-5.4.3-phpbrew_compiled
49.32%9268462069php-5.4.4-phpbrew_compiled
45.35%9353264349php-5.4.5-phpbrew_compiled
41.59%9299465679php-5.4.6-phpbrew_compiled
55.04%9650762248php-5.4.7-phpbrew_compiled
38.52%9337467410php-5.4.8-phpbrew_compiled
47.68%9497564311php-5.4.9-phpbrew_compiled
50%
updown
50%