30

さまざまなバックエンド (sdl、gl、d3d など) で動作する GUI API では、ジェネリック型のイメージを動的にキャストしたいと考えています。

つまり、1 秒あたり約 20 * 60 fps の動的キャストを行うことになります。

動的キャストのコストはどれくらいですか? パフォーマンスに顕著な悪影響があることに気付くでしょうか? 許容レベルのパフォーマンスを維持できる代替手段はありますか?

4

3 に答える 3

25

1200 dynamic_casts per second is not likely to be a major performance problem. Are you doing one dynamic_cast per image, or a whole sequence of if statements until you find the actual type?

If you're worried about performance, the fastest ways to implement polymorphism are:

  • --- fastest ---
  • Function overloading (compile-time polymorphism only)
  • CRTP (compile-time polymorphism only)
  • Tags, switches and static casts (brittle, doesn't support multi-level inheritance, a maintenance headache so not recommended for unstable code)
  • Virtual functions
  • Visitor pattern (inverted virtual function)
  • --- almost as fast ---

In your situation, the visitor pattern is probably the best choice. It's two virtual calls instead of one, but allows you to keep the algorithm implementation separate from the image data structure.

于 2011-04-17T15:47:09.950 に答える
-1

この特定の状況では、コードを整理して、dynamic_cast が 1 回だけ必要になるようにする必要があります。バックエンドは動的に変化しないと思います。

于 2011-04-17T16:07:07.600 に答える
-1

#define を使用して独自のキャストを定義することはできませんか?これは、デバッグ ビルドで dynamic_cast を使用し (したがって、キャストが正しいことがわかります)、リリース ビルドで単純な (MySubclass *) キャストを行うため、パフォーマンス コストはかかりませんか?

于 2011-04-17T15:14:44.693 に答える