2017-02-03 69 views
1

場景:如果這是Facebook或一個巨大的社交網站,並且您必須每秒完成數百萬或數十億次,那麼最快的方法是什麼?修剪首次閃光的最快方法?

這裏是我的網址:

http://www.example.com/profile?id=1/name=bob 

如果我使用的代碼:

$new_url = $_SERVER["REQUEST_URI"]; 

該代碼會顯示此:

/profile?id=1/name=bob

什麼最快的方式(性能)刪除第一個斜槓所以這將是這樣的:profile?id=1/name=bob

我想ltrimtrim,或substr甚至更​​多?感謝

+1

此操作在您的代碼中肯定會影響__NO__性能。 –

+0

@u_mulder對不起,請參閱我的編輯 – pixie123

+1

您可以測試自己,使用'microtime'並查看差異。 –

回答

1

最快的是substrltrimsubstr,並preg_replace測試。 test.php?osd/lskdifo/idlola

順序從最快到最慢:

  1. substr 0.018708944320679
  2. ltrim 0.021075963973999
  3. 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; 

?> 
0

不會有性能差異正因爲如此,你可以使用SUBSTR中acheive它

substr($string, 1);