#include <iostream>
#include <map>
#include <ctime>
struct Base { virtual void foo() {} };
struct A : Base { void foo() {} };
struct B : Base { void foo() {} };
struct C : Base { void foo() {} };
A* protoA = new A; B* protoB = new B; C* protoC = new C;
enum Tag {a, b, c};
std::map<Tag, Base*> protoMap = { {a, protoA}, {b, protoB}, {c, protoC} };
void goo(Base* base) {base->foo();}
void hoo(Tag tag) {protoMap[tag]->foo();}
struct Timer {
const std::clock_t begin;
Timer() : begin (std::clock()) {}
~Timer() {
const std::clock_t end = std::clock();
std::cout << double (end - begin)/CLOCKS_PER_SEC << " seconds." << std::endl;
};
};
int main() {
const long N = 10000000;
{
Timer timer;
for (int i = 0; i < N; i++)
goo(new C); // using vtable
} // 0.445 seconds.
{
Timer timer;
for (int i = 0; i < N; i++)
hoo(c); // using map
} // 0.605 seconds.
std::cin.get();
}
但我的测试只使用三个派生类,我不知道如何定义数千个派生类来进行正确的基准测试。有没有人知道答案已经这样,我不必考虑如何运行更好的测试?哪个执行速度更快?用N个派生类型进行vtable查找,或者用N个元素查找std :: map查找?
我现在处于一种可以轻松使用地图的情况,但试图用vtable查找来提高性能时,需要重新设计我已有的许多东西(向类中添加一个新的数据成员,定义一个新的构造函数,实现访问者模式,等等......)。
你正在比较苹果和橘子;这个“标签”方法如何在现实的多态场景中提供帮助? – 2014-11-09 01:14:15
@Oliver。因为我处于可以轻松使用地图的情况,但是尝试通过vtable查找来提高性能,需要重新设计我已有的许多内容(向类添加新的数据成员,定义新的构造函数,实现访问者模式等)。 – prestokeys 2014-11-09 01:17:01
我认为vtable速度更快,并且是唯一正确的路径,尽管在我的具体情况下我很困难。 – prestokeys 2014-11-09 01:25:37