|
| 1 | +#include <iostream> |
| 2 | +#include <sigc++/signal.h> |
| 3 | +#include <sigc++/functors/mem_fun.h> |
| 4 | +#include <glibmm/timeval.h> |
| 5 | + |
| 6 | +struct foo : public sigc::trackable |
| 7 | +{ |
| 8 | + int bar(int a); |
| 9 | + int c; |
| 10 | +}; |
| 11 | + |
| 12 | +int foo::bar(int a) |
| 13 | +{ |
| 14 | + int b = c; |
| 15 | + c = a; |
| 16 | + return b; |
| 17 | +} |
| 18 | + |
| 19 | +int main() |
| 20 | +{ |
| 21 | + Glib::TimeVal t1, t2; |
| 22 | + |
| 23 | + foo foobar1, foobar2, foobar3, foobar4, foobar5; |
| 24 | + sigc::slot<int,int> slot; |
| 25 | + sigc::signal<int,int> emitter; |
| 26 | + sigc::signal<int,int>::iterator it; |
| 27 | + |
| 28 | + |
| 29 | + // slot benchmark ... |
| 30 | + |
| 31 | + slot = sigc::mem_fun(&foobar1, &foo::bar); |
| 32 | + |
| 33 | + t1.assign_current_time(); |
| 34 | + |
| 35 | + for (int i=0; i < 5000; ++i) |
| 36 | + slot(i); |
| 37 | + |
| 38 | + t2.assign_current_time(); |
| 39 | + t2.subtract(t1); |
| 40 | + |
| 41 | + std::cout << "elapsed time for calling a slot 5000 times: " << t2.tv_sec << "s " << t2.tv_usec << "us" << std::endl; |
| 42 | + |
| 43 | + |
| 44 | + // emission benchmark (zero slots) ... |
| 45 | + |
| 46 | + t1.assign_current_time(); |
| 47 | + |
| 48 | + for (int i=0; i < 1000; ++i) |
| 49 | + emitter(i); |
| 50 | + |
| 51 | + t2.assign_current_time(); |
| 52 | + t2.subtract(t1); |
| 53 | + |
| 54 | + std::cout << "elapsed time for 1000 emissions (0 slots): " << t2.tv_sec << "s " << t2.tv_usec << "us" << std::endl; |
| 55 | + |
| 56 | + |
| 57 | + // emission benchmark (one slot) ... |
| 58 | + |
| 59 | + emitter.connect(mem_fun(&foobar1, &foo::bar)); |
| 60 | + |
| 61 | + t1.assign_current_time(); |
| 62 | + |
| 63 | + for (int i=0; i < 1000; ++i) |
| 64 | + emitter(i); |
| 65 | + |
| 66 | + t2.assign_current_time(); |
| 67 | + t2.subtract(t1); |
| 68 | + |
| 69 | + std::cout << "elapsed time for 1000 emissions (1 slot): " << t2.tv_sec << "s " << t2.tv_usec << "us" << std::endl; |
| 70 | + |
| 71 | + |
| 72 | + // emission benchmark (five slot) ... |
| 73 | + |
| 74 | + emitter.connect(mem_fun(&foobar2, &foo::bar)); |
| 75 | + emitter.connect(mem_fun(&foobar3, &foo::bar)); |
| 76 | + emitter.connect(mem_fun(&foobar4, &foo::bar)); |
| 77 | + emitter.connect(mem_fun(&foobar5, &foo::bar)); |
| 78 | + |
| 79 | + t1.assign_current_time(); |
| 80 | + |
| 81 | + for (int i=0; i < 1000; ++i) |
| 82 | + emitter(i); |
| 83 | + |
| 84 | + t2.assign_current_time(); |
| 85 | + t2.subtract(t1); |
| 86 | + |
| 87 | + std::cout << "elapsed time for 1000 emissions (5 slots): " << t2.tv_sec << "s " << t2.tv_usec << "us" << std::endl; |
| 88 | + |
| 89 | + |
| 90 | + // connection / disconnection benchmark ... |
| 91 | + |
| 92 | + emitter.clear(); |
| 93 | + |
| 94 | + t1.assign_current_time(); |
| 95 | + |
| 96 | + for (int i=0; i < 1000; ++i) |
| 97 | + { |
| 98 | + it = emitter.connect(mem_fun(&foobar1, &foo::bar)); |
| 99 | + it->disconnect(); |
| 100 | + } |
| 101 | + |
| 102 | + t2.assign_current_time(); |
| 103 | + t2.subtract(t1); |
| 104 | + |
| 105 | + std::cout << "elapsed time for 1000 connections/disconnections: " << t2.tv_sec << "s " << t2.tv_usec << "us" << std::endl; |
| 106 | + |
| 107 | +} |
0 commit comments