質問

どのような性能の影響が考慮すべきポイントについて利用がtry-catch諸表php5?

っくりとした一部の古い、一見相反する情報をこのウェブです。多くの枠組みをもとに現在もって取り組んでいたphp4が多くのnicetiesのphp5.いい経験が自分を使用していcatchsとクリアしました。

役に立ちましたか?

解決

気をつけなければいけないと考えるのは、コスト低減をtryブロックがない例外がスローされます別の問題から実際の寺。

場合は例外はスローされる故障の場合、ほとんど確かにどんな性能のまい非常に多くの回行います。だってタイトル(a.k.a:ヘ頭に対するレンガ造りの壁、申請が悪いの問題が遅くなります。だから心配のコストは、例外をスローしないという強制的に利用し通常の制御フローです。

人の回答を掲載しいプロファイリングコードする例外がスローされます。い試験を実施しないが、私は自信を予測することを示すもっと大きなパフォーマンスだけではなくとも、tryブロックを廃棄せずにあります。

もう一つ考えることが巣電話のレベル深層でもよいシングルtry...catchのようにすることが求められまチェックの戻り値と伝播エラー毎に電話します。

の逆の状況にも利用されています。い包装呼び出しのたびに、そのtry...catchブロックコードが遅くなります。とuglier.

他のヒント

ったので退屈してしまったと控えめに以下の(Iのタイミングをコード):

function no_except($a, $b) { 
    $a += $b;
    return $a;
}
function except($a, $b) { 
    try {
        $a += $b;
    } catch (Exception $e) {}
    return $a;
}

利用の異なる二つのループ:

echo 'no except with no surrounding try';
for ($i = 0; $i < NUM_TESTS; ++$i) {
    no_except(5, 7);
}
echo 'no except with surrounding try';
for ($i = 0; $i < NUM_TESTS; ++$i) {
    try {
        no_except(5, 7);
    } catch (Exception $e) {}
}
echo 'except with no surrounding try';
for ($i = 0; $i < NUM_TESTS; ++$i) {
    except(5, 7);
}
echo 'except with surrounding try';
for ($i = 0; $i < NUM_TESTS; ++$i) {
    try {
        except(5, 7);
    } catch (Exception $e) {}
}

と1000000行っWinXPボックスラapacheとPHP5.2.6:

no except with no surrounding try = 3.3296
no except with surrounding try = 3.4246
except with no surrounding try = 3.2548
except with surrounding try = 3.2913

これらの結果は一致していたが同様の割合がいずれの試験でも動く。

結論:追加コードの取扱いに例外ではない以下のコードを無視します。

Try-catchブロックな性能問題の性能ボトルネックからのものを例外オブジェクト。

試験コード:

function shuffle_assoc($array) { 
    $keys = array_keys($array);
    shuffle($keys);
    return array_merge(array_flip($keys), $array);
}

$c_e = new Exception('n');

function no_try($a, $b) { 
    $a = new stdclass;
    return $a;
}
function no_except($a, $b) { 
    try {
        $a = new Exception('k');
    } catch (Exception $e) {
        return $a + $b;
    }
    return $a;
}
function except($a, $b) { 
    try {
        throw new Exception('k');
    } catch (Exception $e) {
        return $a + $b;
    }
    return $a;
}
function constant_except($a, $b) {
    global $c_e;
    try {
        throw $c_e;
    } catch (Exception $e) {
        return $a + $b;
    }
    return $a;
}

$tests = array(
    'no try with no surrounding try'=>function() {
        no_try(5, 7);
    },
    'no try with surrounding try'=>function() {
        try {
            no_try(5, 7);
        } catch (Exception $e) {}
    },
    'no except with no surrounding try'=>function() {
        no_except(5, 7);
    },
    'no except with surrounding try'=>function() {
        try {
            no_except(5, 7);
        } catch (Exception $e) {}
    },
    'except with no surrounding try'=>function() {
        except(5, 7);
    },
    'except with surrounding try'=>function() {
        try {
            except(5, 7);
        } catch (Exception $e) {}
    },
    'constant except with no surrounding try'=>function() {
        constant_except(5, 7);
    },
    'constant except with surrounding try'=>function() {
        try {
            constant_except(5, 7);
        } catch (Exception $e) {}
    },
);
$tests = shuffle_assoc($tests);

foreach($tests as $k=>$f) {
    echo $k;
    $start = microtime(true);
    for ($i = 0; $i < 1000000; ++$i) {
        $f();
    }
    echo ' = '.number_format((microtime(true) - $start), 4)."<br>\n";
}

結果:

no try with no surrounding try = 0.5130
no try with surrounding try = 0.5665
no except with no surrounding try = 3.6469
no except with surrounding try = 3.6979
except with no surrounding try = 3.8729
except with surrounding try = 3.8978
constant except with no surrounding try = 0.5741
constant except with surrounding try = 0.6234

一般的には、例外を想定外の故障、エラーチェックをコード故障に対しての通常のプログラムの状態です。例:

  1. 記録にはないデータベース有効状態になるので、チェックのクエリー結果をメールは、ユーザーが適切に

  2. SQLエラーしようとした場合にのフェッチ記録予期せぬ故障のものではありませんが、プログラムを作りエラーこれは良い場所のための例外-エラーログにエラーログに、メールの管理者、スタックトレース表示のスタイルにエラーメッセージのユーザドさんは、何かを奨機種であっても動作が不安定なんで取り組んでいます。

例外はやはり少々高価でもない限り、お客様のプログラム全体の流れを利用し、性能の差べきではない人たしますのでご連絡ください

私は見つかりませんで何かをTry/Catch性能Googleが単純な試験ループ球エラーの代わりにIF文を329ms vs6msループの5000です。

残念ポストは非常に古いメッセージも読んだのコメントいやや同意できないの差が最小限にな単純なコードで詳細は不明であるTry/Catchを使い特定の部分のコードは必ずしも予測可能なものがとても(未確認)そのシンプルです:

if(isset($var) && is_array($var)){
    foreach($var as $k=>$v){
         $var[$k] = $v+1;
    }
}

よりはるかに高

try{
    foreach($var as $k=>$v){
        $var[$k] = $v+1;
    }
}catch(Exception($e)){
}

この様にして(実験を行わない。):

<?php
//beginning code
try{
    //some more code
    foreach($var as $k=>$v){
        $var[$k] = $v+1;
    }
    //more code
}catch(Exception($e)){
}
//output everything
?>

がより高くなってIfのコード

私が非常に良い質問です!

って試験を実施しくいたパフォーマンス課題-)その通りでした10年前のC++と思う、今日も改善していくからで置する、クリーナーです。

んが恐れを取り囲む私の最初のエントリポイントないものとします。

try {Controller::run();}catch(...)

私はビーチに位置試験をたっぷりの機能を呼び、ビッグが含まれています。...自分で言うのも何ですが、十分に検査するのは、まだやっていますか。

一般的に、彼らをもらい価値あるクリアしました。

ですのでチェック表現の言語は、追い例外がスローされます。

処理時レガシーコードなげ、新しいコードなのでみる混乱した。

幸運を祈っています。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top