2016-12-14 1 views
1

私は、渡された例外をチェックしてbool値を返すメソッドを持っています。例外タイプのパフォーマンスを確認する

現在、私の実装では、しかし、より良いパフォーマンスワイズ辞書検索ではなく、いくつかのOR文とisチェックを使用することです。このよう

private bool ExceptionShouldNotify(Exception e) 
{ 
    return 
    e is FirstCustomException || 
    e is SecondCustomException || 
    e is ThirdCustomException || 
    e is FourthCustomException || 
    e is FifthCustomException || 
    e is SixthCustomException || 
    e is SeventhCustomException; 
} 

のですか?

このような何か:

private bool ExceptionShouldNotify(Exception e) 
{ 
    var dict = new Dictionary<String, int> { 
     { "FirstCustomException", 1 }, 
     { "SecondCustomException", 1 }, 
     { "ThirdCustomException", 1 }, 
     { "FourthCustomException", 1 }, 
     { "FifthCustomException", 1 }, 
     { "SixthCustomException", 1 }, 
     { "SeventhCustomException", 1 } 
    }; 

    return dict.ContainsKey(e.GetType().Name); 
} 
+0

ベンチマークではこれについて何を教えてくれましたか? –

+0

辞書の場合は、むしろ 'HashSet ' –

+0

として実装したいと思います。本当にあなたが失敗の経路にいるときのパフォーマンスについて心配していますか?あなたの顧客は、「よく、落ちましたが、それは本当に速かった」と言っていると思いますか? –

答えて

5

ハードコーディング(第一液)悪い習慣です、私は辞書(第二液)に投票理由ですが、私はアイデアの異なる実装をお勧め:

// HashSet - you don't use Value in the Dictionary, but Key 
    // Type - we compare types, not their names 
    private static HashSet<Type> s_ExceptionsToNotify = new HashSet<Type>() { 
    typeof(FirstCustomException), 
    typeof(SecondCustomException), 
    ... 
    }; 

    // static: you don't use "this" in the method 
    private static bool ExceptionShouldNotify(Exception e) { 
    return s_ExceptionsToNotify.Contains(e.GetType()); 
    } 

例外がキャッチ持つ(どのスタックトレースを含む)あなたはすでに大きなを持っていますオーバーヘッド;そのため、performace(ハッシュの計算と比較して7つの単純な比較)が主な問題ではありません。

+0

私はこのようなものを試しました タイプy = ex。GetType(); スイッチ(Y) { ケースtypeof演算(FirstCustomException): 場合typeof演算(SecondCustomException): 場合typeof演算(ThirdCustomException): ケースtypeof演算(FourthCustomException): 場合typeof演算(FifthCustomException): ケースtypeof演算(SixthCustomException): case typeof(SeventhCustomException): がtrueを返します。 休憩。 デフォルト: falseを返します。 } しかし、エラーとは、「スイッチの式またはケースのラベルは、bool、char、string、integral、enum、または対応するnull可能型でなければなりません。 「タイプ」でスイッチが動作しない理由を教えてください。 –

+0

@Sethu Bala:別に*データ*(例外の種類)と*ソースコード*(データに依存しないはずです)をハードコード*しないでください。 –

+0

私はその点を持っていますが、タイプスイッチではスイッチケースが動作しません。 –

0

はい、それはする必要があります。 文書によれば、メソッドはorステートメントのチェーンがO(N)である間にO(1)に近づく。

https://msdn.microsoft.com/en-us/library/kw5aaea4(v=vs.110).aspx#Remarks

あなたがthe Dictionaryを使用している場合、私はそれはタイプミスに対して保存以上のビットにするためにnameof(Exception)を使用するキーを変更します。

1

チェックする例外タイプがかなりある場合は、辞書を検索するのが理にかなっています。しかし、この文脈ではstringsの使用は避けています。あなたは、例外オブジェクトの種類によって辞書を検索することができます

var dict = new Dictionary<Type, int>() 
{ 
    [typeof(FirstCustomerException)] = 1, 
    [typeof(SecondCustomException)] = 1, 
    ... 
}; 

:あなたは代わりにType種類に依存することができます

try 
{ 
    ... 
} 
catch (Exception ex) 
{ 
    int mapTo; 
    if (dict.TryGetValue(ex.GetType(), out mapTo)) 
    { 
     // Use mapTo value here 
    } 
} 

あなたも、例外と呼ばれるC#6から最新の機能を使用することができますあなたがを呼び出す必要があるため、この実装では、1つのほんの少し遅い

try 
{ 
    ... 
} 
catch (Exception ex) when (dict.ContainsKey(ex.GetType()) 
{ 
    int mapTo = dict[ex.GetType()]; 
    // Use mapTo value here 
} 

:のみにフィルタが辞書に存在するこれらの例外をキャッチを2回実行し、辞書マッピングを2回実行することもできます。しかし、マップにある例外に対してのみ入力され、他のタイプの例外に対しては入力されないため、catchブロックが実際に例外を処理することが保証されるという利点があります。

関連する問題