Before we jump to any conclusions about the results of this code, we need Sunir to come verify that it is a fair test.
It isn't. I'll have to carve out some time to demonstrate what I mean, but that isn't likely to happen while I have a deadline next week ;) --ss
In dispatch.cpp:
#include<iostream> #include<ctime>
class Timeable { public: clock_t run() { clock_t start = ::clock(); for (int i = 0; i < 5000000; i++) reallyRun(); // (Template pattern)
clock_t end = ::clock();
return end - start; }
virtual void reallyRun() = 0; };
class NormalDispatchTimer:
public Timeable {
public: class N''''''ormalClass { public: virtual void run() = 0; };
// It would probably work with just one derived class, but just in case... class F''''''irstNormalClass: public N''''''ormalClass { public: void run() { /* */ } };
class SecondNormalClass:
public NormalClass {
public: void run() { /* */ } };
void reallyRun() { first.run(); second.run(); }
private: F''''''irstNormalClass first; S''''''econdNormalClass second; };
class ManualDispatchTimer:
public Timeable {
public: void reallyRun() { runOn(&first); runOn(&second); }
class M''''''anualClass { public: enum TYPE { F''''''irstType, S''''''econdType, };
unsigned char type; };
class FirstManualClass:
public ManualClass {
public: F''''''irstManualClass(): type(F''''''irstType) { }
unsigned char type;
int someData; };
class SecondManualClass:
public ManualClass {
public: S''''''econdManualClass(): type(S''''''econdType) { }
unsigned char type;
int someOtherData; };
inline void runOn(M''''''anualClass* mcp) { int x; if (M''''''anualClass::F''''''irstType == mcp->type) x = ((F''''''irstManualClass*)mcp)->someData; else if (M''''''anualClass::S''''''econdType == mcp->type) x = ((S''''''econdManualClass*)mcp)->someOtherData; }
private: F''''''irstManualClass first; S''''''econdManualClass second; };
int main() { std::cout << "Normal: " << N''''''ormalDispatchTimer().run() << std::endl << "Manual: " << M''''''anualDispatchTimer().run() << std::endl; }
In the Makefile:
default:
plain optimized
./plain ./optimized
plain:
*.cpp
g++ $< -o plain
optimized:
*.cpp
g++ -O3 $< -o optimized
See original on c2.com