/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <iostream>
#include <vector>
#include <chrono>
using namespace std;
class Element {
public:
virtual ~Element() = default;
};
class CommonRafter : public Element {
};
class HipRafter : public Element {
};
class ValleyRafter : public Element {
};
class Purlin : public Element {
};
class WallPlate : public Element {
};
class CollarBeam : public Element {
};
class Tester {
Element *commonRaftersPtr;
Element *hipRaftersPtr;
Element *valleyRaftersPtr;
Element *purlinsPtr;
Element *wallPlatesPtr;
Element *collarBeamsPtr;
public:
void test1(Element *obj) {
if ( dynamic_cast<CommonRafter *> (obj) ) {
commonRaftersPtr = obj;
return;
}
if ( dynamic_cast<HipRafter *> (obj) ) {
hipRaftersPtr = obj;
return;
}
if ( dynamic_cast<ValleyRafter *> (obj) ) {
valleyRaftersPtr = obj;
return;
}
if ( dynamic_cast<Purlin *> (obj) ) {
purlinsPtr = obj;
return;
}
if ( dynamic_cast<WallPlate *> (obj) ) {
wallPlatesPtr = obj;
return;
}
if ( dynamic_cast<CollarBeam *> (obj) ) {
collarBeamsPtr = obj;
}
}
void test2(CommonRafter *obj) {
commonRaftersPtr = obj;
}
void test2(HipRafter *obj) {
hipRaftersPtr = obj;
}
void test2(ValleyRafter *obj) {
valleyRaftersPtr = obj;
}
void test2(Purlin *obj) {
purlinsPtr = obj;
}
void test2(WallPlate *obj) {
wallPlatesPtr = obj;
}
void test2(CollarBeam *obj) {
collarBeamsPtr = obj;
}
};
int main()
{
constexpr const int len = 1000000;
std::vector<CommonRafter> v1(len);
std::vector<HipRafter> v2(len);
std::vector<ValleyRafter> v3(len);
std::vector<Purlin> v4(len);
std::vector<WallPlate> v5(len);
std::vector<CollarBeam> v6(len);
Tester tester;
auto start = std::chrono::high_resolution_clock::now();
for (int i=0; i<len; i++) {
tester.test1(&v1[i]);
tester.test1(&v2[i]);
tester.test1(&v3[i]);
tester.test1(&v4[i]);
tester.test1(&v5[i]);
tester.test1(&v6[i]);
}
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff1 = end-start;
start = std::chrono::high_resolution_clock::now();
for (int i=0; i<len; i++) {
tester.test2(&v1[i]);
tester.test2(&v2[i]);
tester.test2(&v3[i]);
tester.test2(&v4[i]);
tester.test2(&v5[i]);
tester.test2(&v6[i]);
}
end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff2 = end-start;
std::cout << "diff1: " << diff1.count() << endl;
std::cout << "diff2: " << diff2.count() << endl;
return 0;
}