[ create a new paste ] login | about

Link: http://codepad.org/fGM9dE41    [ raw code | output | fork ]

PHP, pasted on Feb 24:
<?php
$lim = 100000;

$start = microtime(true);

for ($i = 0; $i <= $lim; $i += 1) {
  $str = 'Lorem ipsum dolor    sit amet, consetetur sadipscing elitr, sed    diam nonumy eirmod  tempor invidunt ut    labore et dolore magna aliquyam erat, sed diam voluptua.';
  
  while(strpos($str, '  ') !== false) {
    $str = str_replace('  ', ' ', $str);
  }
}

printf('str_replace: %.5fs' . PHP_EOL, microtime(true) - $start);

/*******************/

$start = microtime(true);

for ($i = 0; $i <= $lim; $i += 1) {
  $str = 'Lorem ipsum dolor    sit amet, consetetur sadipscing elitr, sed    diam nonumy eirmod  tempor invidunt ut    labore et dolore magna aliquyam erat, sed diam voluptua.';
  $str = preg_replace('/\s+/', ' ',$str);
}

printf('preg_replace: %.5fs', microtime(true) - $start);


Output:
1
2
str_replace: 1.55870s
preg_replace: 6.08607s


Create a new paste based on this one


Comments: