Created
February 28, 2015 15:33
-
-
Save tetsuyainfra/85e25c4f44722bfb2e55 to your computer and use it in GitHub Desktop.
c++ 仮想関数呼び出しの影響回避方法 と 回避しなくてもええんじゃね? 覚え書き
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// VSで確認するには C/C++ -> Assemby Outputの項目をAssembly-Only Listing (/FA)に設定 | |
class base | |
{ | |
public: | |
virtual fn() = 0; | |
}; | |
Class A | |
{ | |
public: | |
fn() override { something; } | |
}; | |
base *a = new A(); | |
// vtable経由なので遅くなる | |
a->fn(); | |
// static_castすると早く呼べる | |
static_cast<A*>(a)->A::fn(); | |
メンバ関数ポインタの呼び出しは std::function あたりを検索すると思い出せる。 | |
--- | |
http://stackoverflow.com/questions/14328385/can-vtable-overhead-be-avoided-using-a-static-cast | |
// これでいけんの? | |
template <typename T> | |
void call_fn(base& obj){ | |
static_cast<T&>(obj).T::fn(); | |
} | |
call_fn<A>(obj); | |
// これみると気にしなくて良さそうやな・・・ | |
>Emtpy average =78.686081 clocks | |
>Virtual average =144.732567 clocks | |
>no virtual average =122.781466 clocks | |
>sum=480000000 | |
ゲームとか動画でリアルタイム性求められる物に10%は影響多いのかなぁ? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment