惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
腾讯CDC
M
MIT News - Artificial intelligence
Y
Y Combinator Blog
L
LangChain Blog
Vercel News
Vercel News
云风的 BLOG
云风的 BLOG
GbyAI
GbyAI
Stack Overflow Blog
Stack Overflow Blog
Microsoft Azure Blog
Microsoft Azure Blog
B
Blog RSS Feed
The GitHub Blog
The GitHub Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
B
Blog
P
Proofpoint News Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园_首页
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
aimingoo的专栏
aimingoo的专栏
小众软件
小众软件
IT之家
IT之家
A
About on SuperTechFans
H
Help Net Security

C++

小孩马上高一,但是想学信奥赛 c++,有什么学 c++的书推荐? 真是意想不到的操作:有好几个人一起协作向 C++库 fmtlib 加上了 C11 包装接口,确实能用 基于 C++20 协程编写 gRPC 客户端与服务端 似乎在 C 的领域,让一个新程序“为未来准备好”是一件很麻烦的事 请教各位 centos 7.9 通过 devtoolset 启用 c++14/17 时遇到的链接问题 求大佬指点:Windows 上 c++部署最新 Paddleocr,无法通过内存识字 为 c++ 提供模式匹配 分享一下我个人开源的 C++23 协程网络框架 为什么写 C++的人年龄偏大? 大型 c++项目,在 ai 帮助下完成 Linux 平台移植,可行性多大? 少用 auto 再一次感觉到 C++的恶心 分布式存储 [求助] Linux 有什么好的引入 c++ 第三方库的方案 [求助]请教一个 C++多线程的性能问题 2026 年找 C++的开发工作,应该学习 C++的哪个版本? 分布式系统 使用匿名结构体指针作为常量来杜绝魔数,是否合理/值得? 有没有什么工具可以统计 C++项目里标识符的使用情况? 看到一些 C++ 或者 C#项目 驼峰和下划线一块用,为啥泥? [求助] Linux 系统下动态库卸载后全局变量未重置的问题 交叉编译 asop android adb 最新版的问题 [有偿] 小白, Windows UI Automation TextPattern 检测问题求助 小白问个 vcpkg 相关的问题 记录一次踩坑过程(clion + cmake + vcpkg) 用智能指针管理 ffmpeg 中的数据结构是有必要的吗? 定位重载的插件或者 IDE 想系统的学习 Modern C++,麻烦大佬们推荐一些书籍 困扰几天的问题,这是被 gcc 优化了吗? 好的 c++代码是什么样的
为什么打印模板元编程计算阶乘结果,比打印 for 循环计算阶乘...
zcion · 2024-12-09 · via C++

最近看了 effective c++ 这本书,书中有一种用模板元编程计算阶乘的骚操作,说是可以将计算从运行时转到编译期间,这样可以提高代码的执行效率。

但我尝试了下,发现并没有比使用 for 循环计算阶乘的方法快,反而花费了更多的时间,代码如下:

#include <chrono>
#include <cstdlib>
#include <iostream>
#include <new>
#include <vector>

using std::size_t;

template <unsigned x>
struct fac {
    static const size_t value = x * fac<x - 1>::value;
};

template <>
struct fac<1> {
    static const size_t value = 1;
};

// for 循环计算阶乘
size_t fori(size_t v) {
    size_t tmp = 1;
    for (size_t i = 1; i <= v; i++) {
        tmp = tmp * i;
    }
    return tmp;
}

// 利用模板元编程计算阶乘
constexpr size_t facc() { return fac<901>::value; }

void func() {
    // 模板元编程计算耗时
    auto start = std::chrono::high_resolution_clock::now();

    constexpr auto tmp = facc();
    std::cout << tmp << std::endl;

    auto end = std::chrono::high_resolution_clock::now();
    auto duration =
        std::chrono::duration_cast<std::chrono::nanoseconds>(end - start);
    std::cout << "Elapsed time: " << duration.count() << " ns"
              << std::endl;  // 输出 57466 ns

    // for 循环计算耗时
    auto start1 = std::chrono::high_resolution_clock::now();

    size_t t = fori(901);
    std::cout << tmp << std::endl;

    auto end1 = std::chrono::high_resolution_clock::now();
    auto duration1 =
        std::chrono::duration_cast<std::chrono::nanoseconds>(end1 - start1);
    std::cout << "Elapsed time: " << duration1.count() << " ns"
              << std::endl;  // 输出 1647 ns
}

int main() { func(); }

如果去掉打印,反而是利用模板元编程的更快,确实符合编译期计算提高效率的说法,但这里打印了其结果,反而花费了更多时间。

这是为什么,是编译器自个的优化策略问题还是什么?

编译器版本:gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0