Skip to content

Instantly share code, notes, and snippets.

@ivansky
Last active August 29, 2015 14:16
Show Gist options
  • Save ivansky/c3232bc9e6a7bcd90921 to your computer and use it in GitHub Desktop.
Save ivansky/c3232bc9e6a7bcd90921 to your computer and use it in GitHub Desktop.
<?php
function test($str){
list($var) = explode('/', $str);
return $var;
}
function test2($str){
$var = array_shift(explode('/', $str));
return $var;
}
function test3($str){
preg_match('/^([^\/]*)/', $str, $match);
return $match[1];
}
$str = 'some/test/string';
$time_start = microtime(true);
for($i = 0; $i <= 1000000; $i++){
test($str);
}
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "list() + explode(): {$time} seconds\r\n";
$time_start = microtime(true);
for($i = 0; $i <= 1000000; $i++){
test2($str);
}
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "array_shift() + explode(): {$time} seconds\r\n";
$time_start = microtime(true);
for($i = 0; $i <= 1000000; $i++){
test3($str);
}
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "preg_match(): {$time} seconds\r\n";
/**
* list() + explode(): 1.0205271244049 seconds
* array_shift() + explode(): 2.2764458656311 seconds
* preg_match(): 1.7891898155212 seconds
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment