From 1f8c9b1251421e54d7f397802ca0d6543378bd0c Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Thu, 12 Jan 2023 13:14:03 +0800 Subject: [PATCH] perf: using std functional instead of function pointer --- src/fast_cal.cc | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/src/fast_cal.cc b/src/fast_cal.cc index d12310b..ff65b47 100644 --- a/src/fast_cal.cc +++ b/src/fast_cal.cc @@ -1,5 +1,6 @@ #include #include +#include #include #include "core.h" #include "fast_cal.h" @@ -21,18 +22,25 @@ std::unordered_map cases; class FastCal; class GlobalCal; -template +//template class CoreDemo { public: - typedef void (T::*release_t)(uint64_t); +// typedef void (T::*release_t)(uint64_t); + + std::function 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 callback first time" << std::endl; - (f->*release_func)(++code); +// (f->*release_func)(++code); + release(++code); 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; + } }; @@ -50,11 +58,14 @@ public: } void run() { - auto cd = CoreDemo(); +// auto cd = CoreDemo(); + auto cd = CoreDemo(); + cd.release = std::bind(&FastCal::callback, this, std::placeholders::_1); 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() { - auto cd = CoreDemo(); +// auto cd = CoreDemo(); + auto cd = CoreDemo(); + cd.release = std::bind(&GlobalCal::callback, this, std::placeholders::_1); std::cout << "GlobalCal data = " << data << std::endl; - cd.next(data, this, &GlobalCal::callback); +// cd.next(data, this, &GlobalCal::callback); + cd.next(data); } };