Last active
September 13, 2015 12:07
-
-
Save nineinchnick/84895a560910e3ead9cc to your computer and use it in GitHub Desktop.
Check if adding strpos and strrpos is worth it
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// define benchmarked functions | |
$f1 = function ($string) { | |
for ($i = 0, $count = strlen($string); $i < $count; $i++) { | |
if ($string[$i] === '*') { | |
$string[$i]= mt_rand(0, 1) ? '#' : '?'; | |
} | |
} | |
}; | |
$f2 = function ($string) { | |
if (($pos = strpos($string, '*')) !== false) { | |
for ($i = $pos, $count = strlen($string); $i < $count; $i++) { | |
if ($string[$i] === '*') { | |
$string[$i] = mt_rand(0, 1) ? '#' : '?'; | |
} | |
} | |
} | |
}; | |
$f3 = function ($string) { | |
if (($pos = strpos($string, '*')) !== false) { | |
for ($i = $pos, $count = strrpos($string, '*', $pos); $i < $count; $i++) { | |
if ($string[$i] === '*') { | |
$string[$i] = mt_rand(0, 1) ? '#' : '?'; | |
} | |
} | |
} | |
}; | |
// setup benchmark params | |
$iterations = 100000; | |
$arguments = array( | |
'has' => array('#############****??????????????????'), | |
'hasnt' => array('###############????????????????????'), | |
); | |
$functions = array( | |
'none' => $f1, | |
'strpos' => $f2, | |
'strrpos' => $f3, | |
); | |
$checkValues = false; | |
// run benchmark | |
$benchmark = function ($callable, $arguments, $iterations = 100000) { | |
$start_time = microtime(TRUE); | |
for($i = 0; $i < $iterations; $i++) { | |
$value = call_user_func_array($callable, $arguments); | |
} | |
$end_time = microtime(TRUE); | |
$total_time = $end_time - $start_time; | |
return array($value, $total_time); | |
}; | |
$results = array(); | |
$previous = null; | |
foreach($arguments as $arguments_name => $argument) { | |
foreach($functions as $function_name => $function) { | |
list($value, $time) = $benchmark($function, $argument); | |
if ($checkValues && $previous !== null && $previous !== $value) { | |
echo "error: $function_name ($arguments_name) returned different results:\n"; | |
var_dump($previous, $value); | |
} | |
$previous = $value; | |
$results[$function_name] = $time; | |
} | |
// print results | |
asort($results); | |
$min_time = reset($results); | |
echo "$arguments_name:\n"; | |
foreach($results as $name => $time) { | |
echo str_pad($name.": ", 25, ' ', STR_PAD_LEFT).$time.' ('.(number_format(100*$time/$min_time, 2)).'%)'."\n"; | |
} | |
} | |
echo "\nPerformed $iterations iterations.\n\n"; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'src/autoload.php'; | |
$faker = Faker\Factory::create(); | |
// define benchmarked functions | |
$f1 = function ($string) use ($faker) { | |
preg_replace_callback('/\?/u', [$faker, 'randomLetter'], $string); | |
}; | |
$f2 = function ($string) use ($faker) { | |
if (($pos = strpos($string, '?')) !== false) { | |
for ($i = $pos, $last = strrpos($string, '?', $pos); $i < $last; $i++) { | |
if ($string[$i] === '?') { | |
$string[$i] = $faker->randomLetter(); | |
} | |
} | |
} | |
}; | |
// setup benchmark params | |
$iterations = 100000; | |
$arguments = array( | |
'mixed' => array('#############****??????????????????'), | |
'only' => array('????????????????????'), | |
); | |
$functions = array( | |
'preg_replace' => $f1, | |
'strpos' => $f2, | |
); | |
$checkValues = false; | |
// run benchmark | |
$benchmark = function ($callable, $arguments, $iterations = 100000) { | |
$start_time = microtime(TRUE); | |
for($i = 0; $i < $iterations; $i++) { | |
$value = call_user_func_array($callable, $arguments); | |
} | |
$end_time = microtime(TRUE); | |
$total_time = $end_time - $start_time; | |
return array($value, $total_time); | |
}; | |
$results = array(); | |
$previous = null; | |
foreach($arguments as $arguments_name => $argument) { | |
foreach($functions as $function_name => $function) { | |
list($value, $time) = $benchmark($function, $argument); | |
if ($checkValues && $previous !== null && $previous !== $value) { | |
echo "error: $function_name ($arguments_name) returned different results:\n"; | |
var_dump($previous, $value); | |
} | |
$previous = $value; | |
$results[$function_name] = $time; | |
} | |
// print results | |
asort($results); | |
$min_time = reset($results); | |
echo "$arguments_name:\n"; | |
foreach($results as $name => $time) { | |
echo str_pad($name.": ", 25, ' ', STR_PAD_LEFT).$time.' ('.(number_format(100*$time/$min_time, 2)).'%)'."\n"; | |
} | |
} | |
echo "\nPerformed $iterations iterations.\n\n"; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment