跳转至内容
  • A place to talk about whatever you want

    23 主题
    162 帖子
    StehsaerS
    改进

    经过@yyq252517提醒并测试,直接使用12bit的查找表可以更快:

    consteval std::array<std::array<char, 4>, 4096> hex_to_oct_lookup() noexcept { std::array<std::array<char, 4>, 4096> lookup{}; for (size_t i = 0; i < 4096; ++i) { lookup[i][0] = '0' + ((i >> 0) & 0b111); lookup[i][1] = '0' + ((i >> 3) & 0b111); lookup[i][2] = '0' + ((i >> 6) & 0b111); lookup[i][3] = '0' + ((i >> 9) & 0b111); } return lookup; } // ... std::string hex_to_oct(const std::string& hex) noexcept { const auto hex_length = hex.length(); if (hex_length == 0) return "0"; const auto groups = (hex_length + 2) / 3; const auto oct_digits = groups * 4; std::string res(oct_digits, '0'); for (size_t i = hex_length % 3; i < hex_length; i += 3) { const size_t curr_group = (i + 2) / 3; const uint16_t h0 = hex_lookup[std::bit_cast<uint8_t>(hex[i + 2])]; // [3:0] const uint16_t h1 = hex_lookup[std::bit_cast<uint8_t>(hex[i + 1])]; // [7:4] const uint16_t h2 = hex_lookup[std::bit_cast<uint8_t>(hex[i + 0])]; // [11:8] const uint16_t idx = (h2 << 8) | (h1 << 4) | h0; // [11:0] const auto [o1, o2, o3, o4] = hex_to_oct_lookup_table[idx]; res[curr_group * 4 + 3] = o1; res[curr_group * 4 + 2] = o2; res[curr_group * 4 + 1] = o3; res[curr_group * 4 + 0] = o4; } if (hex_length % 3 != 0) { std::array<uint16_t, 3> h{0, 0, 0}; if (hex_length % 3 == 1) { h[0] = hex_lookup[std::bit_cast<uint8_t>(hex[0])]; } else // hex_length % 3 == 2 { h[1] = hex_lookup[std::bit_cast<uint8_t>(hex[0])]; h[0] = hex_lookup[std::bit_cast<uint8_t>(hex[1])]; } const uint16_t idx = (h[2] << 8) | (h[1] << 4) | h[0]; const auto [o1, o2, o3, o4] = hex_to_oct_lookup_table[idx]; res[3] = o1; res[2] = o2; res[1] = o3; res[0] = o4; } const auto first_non_zero = res.find_first_not_of('0'); if (first_non_zero == std::string::npos) return "0"; return res.substr(first_non_zero); }

    使用和此前一致的测试环境,可以得到:

    Optimized version: 72.16 ms (stddev: 0.45 ms), 1385897448.4948883 hex chars/sec

    比此前快了15%!此前低估了现代CPU的L1缓存系统的能力,看来它处理4096*4byte = 16KiB的查找表也不在话下

    感谢@yyq252517

  • 7 主题
    20 帖子
    SPeakS

    @dustchens 链表结构损坏, 不闭环了 (如果问题解决可以把帖子状态设置为已解决

  • 开源软件 | 开源社区 | 开源理念 | 开源与商业 | 开源可持续发展 等相关话的交流讨论
    注: 这里的"开源"是泛化的共建共享概念, 范围包含 OSI的范围、自由软件、CC等相关内容

    57 主题
    242 帖子
    sunrisepeakS

    参考: http://forum.d2learn.org/post/493

  • 48 主题
    199 帖子
    SPeakS

    之前在什么地方看到过的一个好玩的C++ "语法", 可以让一个数趋向另一个数

    a87e67e0-81ac-4108-9c2a-942c8bc815d9-image.png

    注: 仅供娱乐, 出处已经找不到

  • 20 主题
    56 帖子
    FrozenLemonTeeF

    @SPeak 具体没研究过,不过JetBrains的IDE集成程度比vscode高,可能不太能通过命令或者修改配置文件的方式来修改。如果有朋友有相关了解的可以贴在这里。

  • 一个技术知识分享、学习、交流的社区

    15 主题
    48 帖子
    sunrisepeakS

    @johanvx 版块已创建, 可以检查确认一下是否有话题贴/Topic工具的权限

    https://forum.d2learn.org/category/21/blog-johan-xie
  • Got a question? Ask away!

    4 主题
    14 帖子
    SPeakS

    备注一下使数学公式的使用语法

    单行公式语法 - $ 你的公式 $

    $ log_2^n $

    $ log_2^n $

    多行公式语法 - $$ 你的公式 $$

    $$ log_2^n => log_2^9 = 3 , n = 9 $$

    $$
    log_2^n =>
    log_2^9 = 3, n = 9
    $$

公告栏 | Bulletin Board

欢迎加入d2learn社区 - 社区指南
Welcome to the d2learn Community - Community Guide

一个以 [知识、技术、代码、项目、想法、开源] 相关话题为主导的社区
A community focused on topics related to [knowledge, technology, code, projects, ideas, and open source].


在线用户