/* Trivial test of call times This program times calls of functions doing hardly anything for virtual function (C++) global function called indirectly (C) nonvirtual member function (C++) global function (C) inline member function (C++) macro (C) The calls marked (C++) tests key C++ facilities. Calls marked could be either C or C++ and should have exactly the same performance in either language. The point is that the performance of the C++ facilities are or should be roughly equivalent to equivalent C mechanisms: virtual call == indirect call nonvirtual call == global function call inline call == macro If that is not the case, check that the timings were done correctly (i.e. the timer worked properly, the machine wasn't doing anything else during the test, etc.). If the significant different differences persis complain to your compiler supplier. The program can be given the number of iterations as an argument; the default is 1,000,000. Sample run (on an Ultraspac10 using gcc version egcs-2.91.60 19981201 (egcs-1.1.1 release): [day] c++ -O2 calls.c [day] a.out 5000000 px->f(1) : 0.55 seconds p[1](ps,1) : 0.41 seconds x.f(1) : 0.19 seconds p[1](&s,1) : 0.48 seconds px->g(1) : 0.2 seconds g(ps,1) : 0.2 seconds x.g(1) : 0.19 seconds g(&s,1) : 0.19 seconds X::h(1) : 0.2 seconds h(1) : 0.21 seconds px->k(1) : 0.18 seconds K(ps,1) : 0.19 seconds x.k(1) : 0.18 seconds K(&s,1) : 0.19 seconds [day] - Bjarne Stroustrup - http://www.research.att.com/~bs */ class X { int x; static int st; public: virtual void f(int a); void g(int a); static void h(int a); void k(int i) { x+=i; } }; struct S { int x; }; int glob = 0; void f(S* p, int a); void g(S* p, int a); void h(int a); typedef void (*PF)(S* p, int a); PF p[10] = { g , f }; // inline void k(S* p, int i) { p->x+=i; } #define K(p,i) ((p)->x+=(i)) struct T { const char* s; double t; T(const char* ss, double tt) : s(ss), t(tt) { } T() : s(0), t(0) { } }; #include #include #include using namespace std; int main(int argc, char* argv[]) { int n = (1f(1); v[0] = T("px->f(1)",clock()-t); t = clock(); for (int i = 0; ig(1); v[4] = T("px->g(1)",clock()-t); t = clock(); for (int i = 0; ik(1); v[10] = T("px->k(1)",clock()-t); t = clock(); for (int i = 0; ix += a; } void g(S* p, int a) { p->x += a; } void h(int a) { glob += a; }