How fast is dynamic_cast<>

后端 未结 2 923
北荒
北荒 2021-02-14 15:13

... approximately compared to a typical std::string::operator==()? I give some more details below, I\'m not sure if they are of any relevance. Answer with complexit

2条回答
  •  野的像风
    2021-02-14 16:02

    It depends hugely on your compiler, your particular class hierarchy, the hardware, all sorts of factors. You really need to measure it directly inside your particular application. You can use rdtsc or (on Windows) QueryPerformanceCounter to get a relatively high-precision timer for that purpose. Be sure to time loops or sleds of several thousand dynamic_cast<>s, because even QPC only has a ¼μs resolution.

    In our app, a dynamic_cast<> costs about 1 microsecond, and a string comparison about 3ns/character.

    Both dynamic_cast<> and stricmp() are at the top of our profiles, which means the performance cost of using them is significant. (Frankly in our line of work it's unacceptable to have those functions so high on the profile and I've had to go and rewrite a bunch of someone else's code that uses them.)

提交回复
热议问题