The title's pretty confusing.. Here's a minimal code to sum it up -
#include <iostream>
#include <chrono>
#include <memory>
using namespace std;
using namespace std::chrono;
enum class TYPE {base, child};
class Base {
int n;
protected: Base(int _n) : n(_n) {}
public: virtual TYPE getType(){ return TYPE::base; }
};
class Child: public Base {
int m;
public:
TYPE getType(){ return TYPE::child; }
Child(int _n, int _m) : Base(_n), m(_m) {}
};
int main() {
unique_ptr<Base> b = make_unique<Child>(6, 7);
TYPE tB = TYPE::base;
TYPE tC = TYPE::child;
{
auto t1 = high_resolution_clock::now();
std::cout << (tB == b->Base::getType()) << ", ";
auto t2 = high_resolution_clock::now();
auto duration = duration_cast<microseconds>( t2 - t1 ).count();
cout << "Duration: " << duration << std::endl;
}
{
auto t1 = high_resolution_clock::now();
std::cout << (tC == b->getType()) << ", ";
auto t2 = high_resolution_clock::now();
auto duration = duration_cast<microseconds>( t2 - t1 ).count();
cout << "Duration: " << duration << std::endl;
}
return 0;
}
And the output is as follows -
1, Duration: 12 // first run was 62 1, Duration: 0
I understand how virtual functions might be little slower in comparison to normal(compileTime-resolved) functions, but how come the above function - b->Base::getType() is sooo slower than virtual method - b->getType()? Is it doing two trips, like virtually resolving the function and then going back up to base class? Could anyone help me understand this?
Aucun commentaire:
Enregistrer un commentaire