2

if ステートメントを何度も使用するか、一度使用してデリゲートを使用して IF ステートメントからの出力に基づいて関数を呼び出す方が、パフォーマンスに優れていますか? 答えは明らかにデリゲートであると言いたいのですが、同じことを行う多くの IF ステートメントよりも、何度も何度も異なるメソッドに移動する方が速いか遅いかはわかりません。私はそれを正しく説明したことを願っています。

PSこれを知る必要があるフレームワークは、問題がある場合はXNAです。

4

2 に答える 2

1

You have your trade offs. The best answer was commented already and that is to profile both and then figure it out. IF statements may take more CPU because it has to do the comparisons again and again. On the other hand using delegates takes more memory and it's another object you need to keep around.

Personally what I've like doing the best (when applicable, don't know the full context of your question) is turning your IF ELSE statements into a switch-case. This works really well for state machines and other repetitive processes plus you eliminate all that branching that comes with IFs. However, this is assuming that the values your are checking for are all relatively close in range or else you'll be causing a lot of pain for the compiler.

于 2012-08-05T21:33:48.960 に答える
1

That is a useful technique, sure. Clearly, for only a few if's delegates don't make sense because they are costly and the CPU cannot easily predict the branch target. And for very many if's a one-time initialized delegate makes sense.

It is unclear where the break-even point is. That one needs to be measured.

于 2012-08-05T21:34:00.600 に答える