内联、普通函数、虚函数调用开销比较

measure_functor_call.cpp

cpp 复制代码
#include <iostream>
#include <cctype>  // std::isspace
#include <functional>
#include "profile.h"

/* test1--------begin------- */
struct SpaceChecker{
    bool isSpace(char c){
        return std::isspace(c);
    }
};

PROFILE_REGISTER(countSpace_v1_inline)
size_t   countSpace_v1(const std::string& str){
    PROFILE_CHECK(countSpace_v1_inline);
    SpaceChecker space_checker;
    size_t count{0};
    for (const auto&it: str) {
        if (space_checker.isSpace(it))
        {
            ++count;
        }
    }
    return count;
}
/* test1--------end------- */

/* test2--------begin------- */
struct SpaceCheckerNoInline{
    bool isSpace(char c);
};
PROFILE_NOINLINE bool SpaceCheckerNoInline::isSpace(char c) {
    return std::isspace(c);
}

PROFILE_REGISTER(countSpace_v2_noinline)
size_t countSpace_v2(const std::string& str){
    PROFILE_CHECK(countSpace_v2_noinline);
    SpaceCheckerNoInline space_checker_noinline;
    size_t count{0};
    for (const auto&it: str) {
        if (space_checker_noinline.isSpace(it))
        {
            ++count;
        }
    }
    return count;
}
/* test2--------end------- */

/* test3--------begin------- */
class SpaceCheckerFather{
public:
    virtual ~SpaceCheckerFather(){}
    virtual bool isSpace(char c) = 0;
};

// The purpose of this class is prevent GCC devirtualization optimization
// 如果只有一个子类编译器可能会做devirtualization的动作,即去除虚函数,这样就达不到测试虚函数调用测试的目的了,因此dummy类是为了防止devirtualization用的
class dummy: public SpaceCheckerFather{
public:
    virtual bool isSpace(char) override{
        return false;
    }
};

class SpaceCheckerSon: public SpaceCheckerFather{
public:
    virtual bool isSpace(char c) override;
};
bool SpaceCheckerSon::isSpace(char c){
    return std::isspace(c);
};

PROFILE_REGISTER(countSpace_v3_virtual_call)
size_t countSpace_v3(const std::string& str){
    PROFILE_CHECK(countSpace_v3_virtual_call);
    SpaceCheckerSon space_checker_son;
    size_t count{0};
    for (const auto&it: str) {
        if (space_checker_son.isSpace(it))
        {
            ++count;
        }
    }
    return count;
}
/* test3--------end------- */

/* test4--------begin------- */
std::function<bool(char)> std_func = [](char c)->bool {
    return std::isspace(c);
};

PROFILE_REGISTER(countSpace_v4_std_function)
size_t countSpace_v4(const std::string& str){
    PROFILE_CHECK(countSpace_v4_std_function);
    size_t count{0};
    for (const auto&it: str) {
        if (std_func(it))
        {
            ++count;
        }
    }
    return count;
}
/* test4--------end------- */


auto main()->int{
    constexpr int kLoops = 10000000;

    std::string contents("Welcome to HangZhou, ZheJiang Province.");

    size_t count1{0};
    for (int i = 0; i < kLoops; ++i) {
        count1 = countSpace_v1(contents);
    }
    std::cout << "countSpace_v1's counter is " << count1 << std::endl;

    size_t count2{0};
    for (int i = 0; i < kLoops; ++i) {
        count2 = countSpace_v2(contents);
    }
    std::cout << "countSpace_v2's counter is " << count2 << std::endl;

    size_t count3{0};
    for (int i = 0; i < kLoops; ++i) {
        count3 = countSpace_v3(contents);
    }
    std::cout << "countSpace_v3's counter is " << count3 << std::endl;

    size_t count4{0};
    for (int i = 0; i < kLoops; ++i) {
        count4 = countSpace_v4(contents);
    }
    std::cout << "countSpace_v4's counter is " << count4 << std::endl;
}

profile.h

cpp 复制代码
#ifndef PROFILER_PROFILE_H
#define PROFILER_PROFILE_H

#include <iostream>
#include <vector>
#include <set>
#include <string>
#include <string_view>
#include <format>
#include <assert.h>

#include "profile_rdtsc.h"

#define PF_DEBUG  // open performance test or not

namespace pf {

    struct ProfileInfo {
        int id_;
        int call_count_;
        uint64_t call_duration_;
        std::string func_name_;
    };

    class Profile {
    private:
        Profile() {
#ifdef PF_DEBUG
            constexpr int kTimes = 16;
            for (int i = 0; i < kTimes; ++i) {
                uint64_t begin = rdtsc_benchmark_begin();
                uint64_t end = rdtsc_benchmark_end();
                if (overhead_ > end - begin) {
                    overhead_ = end - begin;   // maybe not the smallest
                }
            }
#endif
            //std::cout << "Profile ctor called.\n";
        }

        ~Profile() {
#ifdef PF_DEBUG
            std::cout << "---> Performance details begin: <---\n";
            std::cout << "Attention, overhead is " << overhead_
                      << " cycles, and it is removed from average duration.\n";
            for (const auto &item: info_) {
                if (!item.call_count_) {
                    continue;
                }
                double average = static_cast<double>(item.call_duration_) / static_cast<double>(item.call_count_) -
                                 static_cast<double>(overhead_);
                if (average < 0) {
                    average = 0.0;  // time cost is smaller than overhead_, because the overhead_ is not accuracy.
                }
                std::cout << "[" << item.func_name_ << ", called " << item.call_count_ << " times, total duration cycle is "
                          << item.call_duration_ << ", average duration cycle is " << average << ".]" << std::endl;

            }
            std::cout << "---> Performance details end. <---\n";
#endif
        }

        Profile(const Profile &) = delete;

        Profile &operator=(const Profile &) = delete;

    public:
        static Profile &getInstance();

        void addInfo(int id, uint64_t duration) {
            assert(id >= 0 && id < static_cast<int>(info_.size()));
            ++info_[id].call_count_;
            info_[id].call_duration_ += duration;
        }

        int registerFunc(const std::string_view func_name) {
            auto [iter, success] = func_name_.insert(std::string(func_name));
            assert(success && "One function can only be registered once!");
            int func_id = static_cast<int>(info_.size());
            info_.push_back({func_id, 0, 0, *iter});

            return func_id;
        }

    private:
        std::set<std::string> func_name_;
        std::vector<ProfileInfo> info_;
        uint64_t overhead_{~0UL};
    };

    class ProfilingChecker {
    public:
        ProfilingChecker(int id) : id_(id), start_time_(rdtsc_benchmark_begin()) {
            //std::cout << "ProfilingChecker ctor called.\n";
        }

        ~ProfilingChecker() {
            uint64_t end_time = rdtsc_benchmark_end();
            uint64_t duration = end_time - start_time_;
            Profile::getInstance().addInfo(id_, duration);

            //std::cout << "ProfilingChecker dtor called.\n";
        }

        ProfilingChecker(const ProfilingChecker &) = delete;

        ProfilingChecker &operator=(const ProfilingChecker &) = delete;

    private:
        int id_;
        uint64_t start_time_;
    };

    int doProfileRegister(const std::string_view func_name);

#ifdef PF_DEBUG
#define PROFILE_REGISTER(func) static const int _pf_id_##func = pf::doProfileRegister(#func);
#define PROFILE_CHECK(func) pf::ProfilingChecker _checker(_pf_id_##func);
#else
    #define PROFILE_REGISTER(func)
#define PROFILE_CHECK(func)
#endif

#ifdef _MSC_VER
#define PROFILE_NOINLINE __declspec(noinline)
#else
#define PROFILE_NOINLINE __attribute__((noinline))
#endif
}
#endif //PROFILER_PROFILE_H

profile.cpp

cpp 复制代码
#include "profile.h"
#include <iostream>

namespace pf {

    Profile &Profile::getInstance() {
        static Profile instance;
        return instance;
    }

    int doProfileRegister(const std::string_view func_name){
        return Profile::getInstance().registerFunc(func_name);
    }
}

profile_rdtsc.h

cpp 复制代码
#ifndef PROFILE_PROFILE_RDTSC_H
#define PROFILE_PROFILE_RDTSC_H

#include <stdint.h>         // uint64_t

#ifndef HAS_HW_RDTSC
#if defined(_M_X64) || defined(_M_IX86) || defined(__x86_64) || defined(__i386)
#define HAS_HW_RDTSC 1
#else
#define HAS_HW_RDTSC 0
#endif
#endif

#if HAS_HW_RDTSC
#ifdef _WIN32
#include <intrin.h>        // __rdtsc/_mm_lfence/_mm_mfence
#elif __has_include(<x86intrin.h>)
#include <x86intrin.h>     // __rdtsc/_mm_lfence/_mm_mfence
#endif
#else
#include <chrono>          // std::chrono::steady_clock/nanoseconds
#endif

// Macro to forbid the compiler from reordering instructions
#ifdef _MSC_VER
#define RDTSC_MEM_BARRIER() _ReadWriteBarrier()
#else
#define RDTSC_MEM_BARRIER() __asm__ __volatile__("" : : : "memory")
#endif

inline uint64_t rdtsc()
{
    RDTSC_MEM_BARRIER();

#if HAS_HW_RDTSC
    uint64_t result = __rdtsc();
#else
    uint64_t result = std::chrono::steady_clock::now().time_since_epoch() /
                      std::chrono::nanoseconds(1);

    //uint64_t result = std::chrono::high_resolution_clock::now().time_since_epoch() /
    //                  std::chrono::nanoseconds(1);
#endif

    RDTSC_MEM_BARRIER();

    return result;
}

#if HAS_HW_RDTSC

inline uint64_t rdtsc_benchmark_begin()
{
    // memory fence, according to x86 architecture,保障开始测试之前让CPU将之前未执行完的指令执行完
    _mm_mfence();
    _mm_lfence();
    uint64_t result = __rdtsc();
    RDTSC_MEM_BARRIER();
    return result;
}

inline uint64_t rdtsc_benchmark_end()
{
    _mm_lfence();
    uint64_t result = __rdtsc();
    RDTSC_MEM_BARRIER();
    return result;
}

#else

inline uint64_t rdtsc_benchmark_begin()
{
    return rdtsc();
}

inline uint64_t rdtsc_benchmark_end()
{
    return rdtsc();
}
#endif
#endif //PROFILE_PROFILE_RDTSC_H

输出结果如下:
由此可以看出,普通函数和虚函数开销差异本身并不大,内联可以降低开销。

相关推荐
程序猿进阶2 分钟前
堆外内存泄露排查经历
java·jvm·后端·面试·性能优化·oom·内存泄露
我们的五年5 分钟前
【Linux课程学习】:进程程序替换,execl,execv,execlp,execvp,execve,execle,execvpe函数
linux·c++·学习
做人不要太理性32 分钟前
【C++】深入哈希表核心:从改造到封装,解锁 unordered_set 与 unordered_map 的终极奥义!
c++·哈希算法·散列表·unordered_map·unordered_set
程序员-King.40 分钟前
2、桥接模式
c++·桥接模式
chnming198744 分钟前
STL关联式容器之map
开发语言·c++
程序伍六七1 小时前
day16
开发语言·c++
小陈phd1 小时前
Vscode LinuxC++环境配置
linux·c++·vscode
火山口车神丶1 小时前
某车企ASW面试笔试题
c++·matlab
是阿建吖!2 小时前
【优选算法】二分查找
c++·算法
Ajiang28247353044 小时前
对于C++中stack和queue的认识以及priority_queue的模拟实现
开发语言·c++