]> git.tdb.fi Git - ext/sigc++-2.0.git/blob - tests/benchmark.cc
Adjust the name of the library to match upstream
[ext/sigc++-2.0.git] / tests / benchmark.cc
1 /* Copyright 2003 - 2016, The libsigc++ Development Team
2  *  Assigned to public domain.  Use as you wish without restriction.
3  */
4
5 #include <iostream>
6 #include <sigc++/signal.h>
7 #include <sigc++/functors/mem_fun.h>
8 #include <boost/timer/timer.hpp>
9
10 const int COUNT = 10000000;
11
12 struct foo : public sigc::trackable
13 {
14   int bar(int a);
15   int c;
16 };
17
18 int foo::bar(int a)
19 {
20   int b = c;
21   c = a;
22   return b;
23 }
24
25 void test_slot_call()
26 {
27   foo foobar1;
28   sigc::signal<int(int)>::iterator it;
29
30   // slot benchmark ...
31
32   sigc::slot<int(int)> slot = sigc::mem_fun(foobar1, &foo::bar);
33
34   std::cout << "elapsed time for calling a slot " << COUNT << " times:" << std::endl;
35   boost::timer::auto_cpu_timer timer;
36
37   for (int i=0; i < COUNT; ++i)
38     slot(i);
39 }
40
41 void test_signal_emit()
42 {
43   sigc::signal<int(int)> emitter;
44
45   std::cout << "elapsed time for " << COUNT << " emissions (0 slots):" << std::endl;
46   boost::timer::auto_cpu_timer timer;
47
48   for (int i=0; i < COUNT; ++i)
49     emitter(i);
50 }
51
52 void test_connected_signal_emit()
53 {
54   foo foobar1;
55   sigc::signal<int(int)> emitter;
56   emitter.connect(mem_fun(foobar1, &foo::bar));
57
58   std::cout << "elapsed time for " << COUNT << " emissions (1 slot):" << std::endl;
59   boost::timer::auto_cpu_timer timer;
60
61   for (int i=0; i < COUNT; ++i)
62     emitter(i);
63 }
64
65 void test_connected_multiple_signal_emit()
66 {
67   foo foobar1, foobar2, foobar3, foobar4, foobar5;
68
69   sigc::signal<int(int)> emitter;
70   emitter.connect(mem_fun(foobar2, &foo::bar));
71   emitter.connect(mem_fun(foobar3, &foo::bar));
72   emitter.connect(mem_fun(foobar4, &foo::bar));
73   emitter.connect(mem_fun(foobar5, &foo::bar));
74
75   std::cout << "elapsed time for " << COUNT << " emissions (5 slots):" << std::endl;
76   boost::timer::auto_cpu_timer timer;;
77
78   for (int i=0; i < COUNT; ++i)
79     emitter(i);
80 }
81
82 void test_connect_disconnect()
83 {
84   foo foobar1;
85   sigc::signal<int(int)> emitter;
86   sigc::signal<int(int)>::iterator it;
87
88   std::cout << "elapsed time for " << COUNT << " connections/disconnections:" << std::endl;
89   boost::timer::auto_cpu_timer timer;
90
91   for (int i=0; i < COUNT; ++i)
92     {
93       it = emitter.connect(mem_fun(foobar1, &foo::bar));
94       it->disconnect();
95     }
96 }
97
98 int main()
99 {
100   // slot benchmark ...
101   test_slot_call();
102
103   // emission benchmark (zero slots) ...
104   test_signal_emit();
105
106   // emission benchmark (one slot) ...
107   test_connected_signal_emit();
108
109   // emission benchmark (five slot) ...
110   test_connected_multiple_signal_emit();
111
112   // connection / disconnection benchmark ...
113   test_connect_disconnect();
114 }