最快的是substr
時ltrim
,substr
,並preg_replace
測試。 test.php?osd/lskdifo/idlola
順序從最快到最慢:
substr
0.018708944320679
ltrim
0.021075963973999
preg_replace
0.049320220947266
下面是測試:這裏是URL我測試了它
substr
:
<?php
$x = 0;
$start = microtime(true);
while ($x<=100000) {
substr($_SERVER['REQUEST_URI'], 1);
$x++;
}
$time_elapsed_secs = microtime(true) - $start;
echo substr($_SERVER['REQUEST_URI'], 1);
echo $time_elapsed_secs;
?>
ltrim
:
<?php
$x=0;
$start = microtime(true);
while ($x<=100000) {
ltrim($_SERVER['REQUEST_URI'], '/');
$x++;
}
$time_elapsed_secs = microtime(true) - $start;
echo ltrim($_SERVER['REQUEST_URI'], '/');
echo $time_elapsed_secs;
?>
preg_replace
:
<?php
$link = $_SERVER['REQUEST_URI'];
$x=0;
$start = microtime(true);
while ($x<=100000) {
preg_replace('/^\//', '', $link);
$x++;
}
$time_elapsed_secs = microtime(true) - $start;
echo preg_replace('/^\//', '', $link);
echo $time_elapsed_secs;
?>
此操作在您的代碼中肯定會影響__NO__性能。 –
@u_mulder對不起,請參閱我的編輯 – pixie123
您可以測試自己,使用'microtime'並查看差異。 –