/* Some performance benchmarks of boost.signals vs signals2 */ // Copyright Frank Mori Hess 2008. // Distributed under the Boost Software License, Version // 1.0. (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) #include #include #include #include #include #include typedef boost::signals2::signal, int, std::less, boost::function, boost::function, boost::signals2::dummy_mutex> new_signal_type; typedef boost::signal old_signal_type; class myslot { public: void operator()(int) {}; }; class trackable_slot: public myslot, public boost::signals::trackable {}; template void benchmark_invocation(unsigned num_connections) { static const unsigned num_invocations = 1000000; Signal signal; std::cout << num_connections << " connections, invoking " << num_invocations << " times: "; unsigned n; for(n = 0; n < num_connections; ++n) { signal.connect(myslot()); } { boost::progress_timer timer; unsigned i; for(i = 0; i < num_invocations; ++i) { signal(0); } } } void benchmark_old_tracked_invocation(unsigned num_connections) { static const unsigned num_invocations = 1000000; old_signal_type signal; std::cout << "boost::signal, " << num_connections << " connections, tracking enabled, invoking " << num_invocations << " times: "; unsigned n; trackable_slot tslot; for(n = 0; n < num_connections; ++n) { signal.connect(tslot); } { boost::progress_timer timer; unsigned i; for(i = 0; i < num_invocations; ++i) { signal(0); } } } void benchmark_new_tracked_invocation(unsigned num_connections) { static const unsigned num_invocations = 1000000; new_signal_type signal; std::cout << "boost::signals2::signal, " << num_connections << " connections, tracking enabled, invoking " << num_invocations << " times: "; boost::shared_ptr trackable_ptr(new int(0)); unsigned n; for(n = 0; n < num_connections; ++n) { new_signal_type::slot_type slot((myslot())); slot.track(trackable_ptr); signal.connect(slot); } { boost::progress_timer timer; unsigned i; for(i = 0; i < num_invocations; ++i) { signal(0); } } } template class connection_type; template<> class connection_type { public: typedef boost::signals::connection type; }; template<> class connection_type { public: typedef boost::signals2::connection type; }; template void benchmark_connect_disconnect() { static const unsigned num_connections = 1000000; std::vector::type> connections(num_connections); Signal signal; std::cout << "connecting " << num_connections << " connections then disconnecting: "; unsigned n; { boost::progress_timer timer; for(n = 0; n < num_connections; ++n) { connections.at(n) = signal.connect(myslot()); } for(n = 0; n < num_connections; ++n) { connections.at(n).disconnect(); } } } int main(int argc, const char **argv) { { std::cout << "boost::signals2::signal, "; benchmark_invocation(1); } { std::cout << "boost::signal, "; benchmark_invocation(1); } { std::cout << "boost::signals2::signal, "; benchmark_invocation(10); } { std::cout << "boost::signal, "; benchmark_invocation(10); } benchmark_old_tracked_invocation(1); benchmark_new_tracked_invocation(1); benchmark_old_tracked_invocation(10); benchmark_new_tracked_invocation(10); { std::cout << "boost::signals2::signal, "; benchmark_connect_disconnect(); } { std::cout << "boost::signal, "; benchmark_connect_disconnect(); } }