Skip to content

Instantly share code, notes, and snippets.

@hipertracker
Created March 29, 2009 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hipertracker/87516 to your computer and use it in GitHub Desktop.
Save hipertracker/87516 to your computer and use it in GitHub Desktop.
#include <ctime>
#include <iostream>
using namespace std;
static int i = 0;
struct A {
virtual void perform() {
i++;
}
};
struct B : public A {
void perform() {
i += 4;
}
};
void bench();
int main() {
bench()
bench()
bench()
bench()
bench()
cout << i << endl;
return 0;
}
void bench() {
clock_t cstart = clock();
A*a = new A();
A*b = new B();
for (int i=0;i<400000000;i++) {
A*x = (i & 1) == 0 ? a : b;
x->perform();
}
delete a;
delete b;
clock_t cend = clock();
double millis = 1000.0 * (cend - cstart) / CLOCKS_PER_SEC;
cout << millis << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment