2009-07-15 23 views
3

我正在試圖優化PHP應用程序中的特定函數,並愚蠢地認爲'if'語句中的布爾查找比字符串比較更快。但要檢查它,我使用microtime進行了一個簡短測試(見下文)。令我驚訝的是,字符串查找更快。PHP字符串與布爾速度測試

我的測試有什麼問題(我連接太多的咖啡,所以我懷疑我自己的代碼)?如果沒有,我會對PHP中有關字符串與布爾型查找的任何評論感興趣。

第一個測試(布爾查找)的結果是0.168秒。

第二個測試(字符串查找)的結果是0.005秒。

<?php 
    $how_many = 1000000; 
    $counter1 = 0; 
    $counter2 = 0; 

    $abc = array('boolean_lookup'=>TRUE, 'string_lookup'=>'something_else'); 

    $start = microtime(); 
    for ($i = 0; $i < $how_many; $i++) 
    { 
     if ($abc['boolean_lookup']) 
     { 
      $counter1++; 
     } 
    } 

    echo ($start - microtime()); 

    echo '<hr>'; 

    $start = microtime(); 
    for ($i = 0; $i < $how_many; $i++) 
    { 
     if ($abc['string_lookup'] == 'something_else') 
     { 
      $counter2++; 
     } 
    } 

    echo ($start - microtime()); 
+1

難道你不想要的差異使用microtime() - $ start? $ start-microtime()應該是負值。 – BaroqueBobcat 2009-07-15 02:30:39

回答

6

是的,你有太多的咖啡。你需要使用microtime(true)否則你的日期計算工作在毫秒,但完全忽略秒。此外,使用當前時間 - 開始時間來衡量時間,而不是開始時間 - 當前時間,否則你會得到一個負時間。請嘗試使用以下代碼:

<?php 

$how_many = 5000000; 
$counter1 = 0; 
$counter2 = 0; 

$abc = array('boolean_lookup'=>TRUE, 'string_lookup'=>'something_else'); 

$start = microtime(true); 
for($i = 0; $i < $how_many; $i++) 
{ 
    if($abc['boolean_lookup']) 
    { 
     $counter1++; 
    } 

} 

echo "FIRST: ", (microtime(true) - $start), "\n"; 

$start = microtime(true); 
for($i = 0; $i < $how_many; $i++) 
{ 
    if($abc['string_lookup'] == 'something_else') 
    { 
     $counter2++; 
    } 

} 

echo "SECOND: ", (microtime(true) - $start), "\n"; 
+1

謝謝!我會放下咖啡:) – 2009-07-15 02:31:43

0

您可能需要稍微調整bool比較。

如果($ var)不是bool比較(與「字符串比較」相比),則執行一般操作。

if($abc['boolean_lookup'] == TRUE)if($abc['boolean_lookup'] === TRUE)(2對3等號)再次嘗試測試。

我做了一個測試,包括bool比較,以及三等於符號比較。 此外還有一個循環來做3次,因爲結果可能因多種外部因素而異。

由於腳本excution的總時間是相當長的這種方式我lowereed的$how_many = 5000000;$how_many = 3000000;

下面是最終腳本:

<?php 
function DoTest(){ 

    $how_many = 3000000; 
    $counter1 = 0; 
    $counter2 = 0; 
    $counter3 = 0; 
    $counter4 = 0; 
    $counter5 = 0; 
    $counter6 = 0; 

    $abc = array('boolean_lookup'=>TRUE, 'string_lookup'=>'something_else'); 

    $start = microtime(true); 
    for($i = 0; $i < $how_many; $i++){ 
     if($abc['boolean_lookup']){ 
      $counter1++; 
     } 

    } 
    echo "GENERAL-IF ON A BOOL: ", (microtime(true) - $start)*10, "<br />\n"; 

    $start = microtime(true); 
    for($i = 0; $i < $how_many; $i++){ 
     if($abc['string_lookup']){ 
      $counter2++; 
     } 

    } 
    echo "GENERAL-IF ON A STRING: ", (microtime(true) - $start)*10, "<br />\n"; 

    $start = microtime(true); 
    for($i = 0; $i < $how_many; $i++){ 
     if($abc['boolean_lookup'] == TRUE){ 
      $counter3++; 
     } 

    } 
    echo "TWO-EQUALL-IF ON A BOOL : ", (microtime(true) - $start)*10, "<br />\n"; 

    $start = microtime(true); 
    for($i = 0; $i < $how_many; $i++){ 
     if($abc['string_lookup'] == 'something_else'){ 
      $counter4++; 
     } 

    } 
    echo "TWO-EQUALL-IF ON A STRING : ", (microtime(true) - $start)*10, "<br />\n"; 

    $start = microtime(true); 
    for($i = 0; $i < $how_many; $i++){ 
     if($abc['boolean_lookup'] === TRUE){ 
      $counter5++; 
     } 

    } 
    echo "THREE-EQUALL-IF ON A BOOL : ", (microtime(true) - $start)*10, "<br />\n"; 

    $start = microtime(true); 
    for($i = 0; $i < $how_many; $i++){ 
     if($abc['string_lookup'] === 'something_else'){ 
      $counter6++; 
     } 

    } 
    echo "THREE-EQUALL-IF ON A STRING : ", (microtime(true) - $start)*10, "<br />\n"; 

} 

$number_of_tests = 3; 
for($i = 0; $i < $number_of_tests; $i++){ 
    echo "<br />\n<br />\n== Test #".($i+1)."<br />\n"; 
    DoTest(); 
} 
?> 

而且結果:

== Test #1 
GENERAL-IF ON A BOOL: 7.61245965958 
GENERAL-IF ON A STRING: 7.49043941498 
TWO-EQUALL-IF ON A BOOL : 8.92991065979 
TWO-EQUALL-IF ON A STRING : 10.3996396065 
THREE-EQUALL-IF ON A BOOL : 8.02039146423 
THREE-EQUALL-IF ON A STRING : 9.25590991974 


== Test #2 
GENERAL-IF ON A BOOL: 7.74684906006 
GENERAL-IF ON A STRING: 7.58201122284 
TWO-EQUALL-IF ON A BOOL : 8.90240907669 
TWO-EQUALL-IF ON A STRING : 10.2967596054 
THREE-EQUALL-IF ON A BOOL : 8.08442115784 
THREE-EQUALL-IF ON A STRING : 9.2577290535 


== Test #3 
GENERAL-IF ON A BOOL: 7.63362884521 
GENERAL-IF ON A STRING: 7.5103187561 
TWO-EQUALL-IF ON A BOOL : 8.92127037048 
TWO-EQUALL-IF ON A STRING : 10.4210495949 
THREE-EQUALL-IF ON A BOOL : 8.02319049835 
THREE-EQUALL-IF ON A STRING : 9.25379991531 

所以,我的結論是,當進行比較(等號)時,bool毫無疑問是贏家。

但是,當做一個簡單的普通if() - 語句時,字符串會返回更早的布爾值。

改天我會看看是否有在返回true或false(即它需要更長的時間來檢測相反?)

問候, Krinkle