Browse Source

perf: using std functional instead of function pointer

master
Dnomd343 2 years ago
parent
commit
1f8c9b1251
  1. 32
      src/fast_cal.cc

32
src/fast_cal.cc

@ -1,5 +1,6 @@
#include <iostream> #include <iostream>
#include <unordered_map> #include <unordered_map>
#include <functional>
#include <queue> #include <queue>
#include "core.h" #include "core.h"
#include "fast_cal.h" #include "fast_cal.h"
@ -21,18 +22,25 @@ std::unordered_map<uint64_t, fast_cal_t> cases;
class FastCal; class FastCal;
class GlobalCal; class GlobalCal;
template<typename T> //template<typename T>
class CoreDemo { class CoreDemo {
public: public:
typedef void (T::*release_t)(uint64_t); // typedef void (T::*release_t)(uint64_t);
std::function<void(uint64_t)> release;
// void next(uint64_t code, T *f, release_t release_func) {
void next(uint64_t code) {
void next(uint64_t code, T *f, release_t release_func) {
std::cout << "Core get code = " << code << std::endl; std::cout << "Core get code = " << code << std::endl;
std::cout << "Core callback first time" << std::endl; std::cout << "Core callback first time" << std::endl;
(f->*release_func)(++code); // (f->*release_func)(++code);
release(++code);
std::cout << "Core callback second time" << std::endl; std::cout << "Core callback second time" << std::endl;
(f->*release_func)(++code); // (f->*release_func)(++code);
release(++code);
std::cout << "Core function exit" << std::endl; std::cout << "Core function exit" << std::endl;
} }
}; };
@ -50,11 +58,14 @@ public:
} }
void run() { void run() {
auto cd = CoreDemo<FastCal>(); // auto cd = CoreDemo<FastCal>();
auto cd = CoreDemo();
cd.release = std::bind(&FastCal::callback, this, std::placeholders::_1);
std::cout << "FastCal data = " << data << std::endl; std::cout << "FastCal data = " << data << std::endl;
cd.next(data, this, &FastCal::callback); // cd.next(data, this, &FastCal::callback);
cd.next(data);
} }
}; };
@ -72,11 +83,14 @@ public:
} }
void run() { void run() {
auto cd = CoreDemo<GlobalCal>(); // auto cd = CoreDemo<GlobalCal>();
auto cd = CoreDemo();
cd.release = std::bind(&GlobalCal::callback, this, std::placeholders::_1);
std::cout << "GlobalCal data = " << data << std::endl; std::cout << "GlobalCal data = " << data << std::endl;
cd.next(data, this, &GlobalCal::callback); // cd.next(data, this, &GlobalCal::callback);
cd.next(data);
} }
}; };

Loading…
Cancel
Save