-1

たぶんこれはばかげた質問ですが、何が速いのでしょうか?

<?php

function getCss1 ($id = 0) {
    if ($id == 1) {
        return 'red';
    } else if ($id == 2) {
        return 'yellow';
    } else if ($id == 3) {
        return 'green';
    } else if ($id == 4) {
        return 'blue';
    } else if ($id == 5) {
        return 'orange';
    } else {
        return 'grey';
    }
}

function getCss2 ($id = 0) {
    $css[] = 'grey';
    $css[] = 'red';
    $css[] = 'yellow';
    $css[] = 'green';
    $css[] = 'blue';
    $css[] = 'orange';
    return $css[$id];
}

echo getCss1(3);
echo getCss2(3);
?>

ifステートメントの方が速いと思いますが、質問したいと思います。

4

9 に答える 9

6

getCss1(if ステートメント) は、私のベンチマークの約 2 倍の速さgetCss2(配列アクセス) です。

からの結果microtime():

getCss1 called 10,000 times in 0.016569852828979 seconds
getCss2 called 10,000 times in 0.037255048751831 seconds

コメントによると、翻訳配列を使用することの保守性については同意します。$css関数のオーバーヘッドと配列の再宣言がなくなるため、直接アクセスする方がはるかに高速です。

getCss1 called 10,000 times in 0.016607999801636 seconds
$css accessed 10,000 times in 0.0026898384094238 seconds

注: Mac OS X 10.8 で PHP 5.3.15 を実行しています。$idまた、実行パスをテストするために呼び出し順序を変更しました。

于 2012-11-15T22:46:05.597 に答える
3

私はそれを計った:

$t = microtime(true);
for($i = 0; $i < 10000000; $i++){
    getCss1(3);
}
echo "getCss1: ".(microtime(true) - $t)."\n";

$t = microtime(true);
for($i = 0; $i < 10000000; $i++){
    getCss2(3);
}
echo "getCss2: ".(microtime(true) - $t)."\n";

getCss2 関数の外で $css を定義し、グローバルな $css を使用すると、関数は getCss1 よりも遅くなりますが、以前のバージョンよりもはるかに高速です。

$css[] = 'grey';
$css[] = 'red';
$css[] = 'yellow';
$css[] = 'green';
$css[] = 'blue';
$css[] = 'orange';
function getCss3 ($id = 0) {
    global $css;
    return $css[$id];
}

$t = microtime(true);
for($i = 0; $i < 10000000; $i++){
    getCss3(3);
}
echo "getCss3: ".(microtime(true) - $t)."\n";

結果 (Windows 7、AMD Phenom II: 3.6Ghz、PHP 5.3.3):

getCss1: 3.7735629081726
getCss2: 14.683212995529
getCss3: 4.2553169727325

異なる $id の使用:

$id = 6:

getCss1: 4.2732820510864
getCss2: 32.388185024261
getCss3: 20.429337024689

$id = 0:

getCss1: 4.3480041027069
getCss2: 14.638042926788
getCss3: 4.2784569263458

そのため、if の速度は値の位置とは無関係に見えますが、「低」キーにアクセスする場合は配列アクセスの方がはるかに高速です。

于 2012-11-15T22:55:32.380 に答える
1

getCss1(3) は常に 3 つの条件をチェックする必要があることを忘れないでください。getCss(3) は、位置 3 で配列の値を取得する必要があります...

確かに:配列へのアクセスはより高速です..

これは私のテストアプリケーションでした:

function getCss1($id = 0) {
  if ($id == 1) {
    return 'red';
  } else if ($id == 2) {
    return 'yellow';
  } else if ($id == 3) {
    return 'green';
  } else if ($id == 4) {
    return 'blue';
  } else if ($id == 5) {
    return 'orange';
  } else {
    return 'grey';
  }
}

function getCss2($id = 0) {
  static $css;
  if ($css === null) {
    $css[] = 'grey';
    $css[] = 'red';
    $css[] = 'yellow';
    $css[] = 'green';
    $css[] = 'blue';
    $css[] = 'orange';
  }
  return $css[$id];
}

$start = microtime(true);
for ($i = 0; $i < 10000; $i++) {
  $x = getCss1($i%6);
}
$end = microtime(true);
echo 'getCss1: ' . ($end-$start) . "\n";

$start = microtime(true);
for ($i = 0; $i < 10000; $i++) {
  $x = getCss2($i%6);
}
$end = microtime(true);
echo 'getCss2: ' . ($end-$start) . "\n";
于 2012-11-15T23:21:00.157 に答える
1

タイミングの取り方はこちら!

これら 2 つの関数を 1 つのファイルに保持します。関数を定義したら、一方のタイマーを開始し、次にもう一方のタイマーを開始します。例:

// Run function 1
$time_start1 = microtime();
getCss1 (2);
$time_end1 = microtime();
$time1 = $time_end1 - $time_start1;

echo "getCss1 function executed in $time1 seconds\n";

// Run function 2
$time_start2 = microtime();
getCss2 (2);
$time_end2 = microtime();
$time2 = $time_end2 - $time_start2;

echo "getCss2 function executed in $time2 seconds\n";
于 2012-11-15T22:47:57.723 に答える
1

2 番目の関数が遅くなる唯一の理由は、メモリへのアクセスに必要な時間です。ただし、配列をプロセッサのキャッシュに格納できるため、時間が大幅に短縮されます。また、2 番目の関数は、コール スタックでのジャンプが少なくなります。

理論的には、現在のマシン構成とプロセッサ アーキテクチャに依存するため、判断する方法はありません。

速度テストを行う場合は、必ず多くのテストを行い、平均速度値を比較してください...

于 2012-11-15T22:54:07.380 に答える
0

速度は の値に依存する必要があります。$idこれが 6 の場合、すべての if ステートメントを反復処理し、配列からインデックス付きの値を取得する方が高速になる可能性があるためです。

于 2012-11-15T22:50:50.650 に答える
0

最初: if/switch ステートメントは実際には動的ではなく、配列はメモリを消費するためです (はい、if ステートメントがおそらくそうであることは知っていますが、はるかに小さいです)。

おそらく戻り値を除いて、サーバーのRAM(別名変数)に何も割り当てていないため、変数をメモリに保持する必要がないifステートメント。

その配列内のすべてのアイテムは、最初にメモリに格納され、次にキー値メソッドで使用できます。

これは本当に小さすぎて問題にはなりませんが、さまざまな大きなことをしようとしているデータベースから動的なキー => 値の配列を作成するために何度もメモリを使い果たしました。このような場合は、switch ステートメントを使用することをお勧めします。

于 2012-11-15T22:51:37.690 に答える
0

You're correct. If statement is faster, cause it's compares all the variants in the worthest situation. When you use an array, it needs time and memory to create all the variants in every situation. But! The variant with array is more readable and usable and if array not big and his values are constant (not result of functions!), it's better to use array-way.

于 2012-11-15T22:53:35.837 に答える
0

if の代わりに switch を使用することもできます。Switch は if ステートメントよりも高速です

switch ($id){
case 1: return "red";
break;
case 2: return "yellow";
break;
case 3: return "green";
...
}
于 2014-10-05T19:51:44.797 に答える