C ++ virtual call versus boost :: function call speedwise

I wanted to know how fast this is a virtual function call with one inheritance compared to one boost :: function call. Are they almost the same in performance or is the boost :: function function slower?

I know that performance can vary from case to case, but is it usually faster, and how important is it?

Thanks Guillerme

- edit

The KennyTM test was convincing enough for me. The boost :: function doesn't seem much slower than vcall for my own purposes. Thanks.

+5
source share
1 answer

10 9 times.


A:

struct X {
            virtual ~X() {}
        virtual void do_x() {};
};
struct Y : public X {}; // for the paranoid.

int main () {
        Y* x = new Y;
        for (int i = 100000000; i >= 0; -- i)
                x->do_x();
        delete x;
        return 0;
}

B: ( 1.41):

#include <boost/function.hpp>

struct X {
    void do_x() {};
};

int main () {
    X* x = new X;
    boost::function<void (X*)> f;
    f = &X::do_x;
    for (int i = 100000000; i >= 0; -- i)
        f(x);
    delete x;
    return 0;
}

g++ -O3, time,

  • A 0,30 .
  • B 0,54 .

, , , f NULL. , boost::function, 2,4 ( 2 ), do_x() . , boost::function.

+7

All Articles