用fast_float库加速字面浮点数字符串转换为浮点数

在网上看到一篇文章redis fast_float, 其中介绍了快速浮点数转换库,下载源代码来试用了,真的比系统的strtod快出不少。

测试程序fast_float_test.cpp复制自上述文章,以防丢失。

#include <iostream>
#include <vector>
#include <chrono>
#include <cstring>
#include "fast_float/fast_float.h"

const char* test_strings[] = {
    "3.14159", "2.7182818284", "1.0e10", "6.022e23",
    "2.2250738585072014e-308",
    "0.0", "123456789.987654321", "9.999999999999999e-10", "1e-10"
};

constexpr size_t NUM_STRINGS = std::size(test_strings);
constexpr int ITERATIONS = 1000000;

volatile double sink = 0.0; // 防止优化

int main(){
    // 测试 strtod 性能
    {
        auto start = std::chrono::high_resolution_clock::now();

        for (int i = 0; i < ITERATIONS; ++i){
            for (size_t j = 0; j < NUM_STRINGS; ++j){
                double val = strtod(test_strings[j], nullptr);
                sink += val * 0; // 乘0避免溢出,但又用到 val 防止优化
            }
        }

        auto end = std::chrono::high_resolution_clock::now();
        auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
        std::cout << "strtod total time: " << ms << " ms\n";
    }

    // 测试 fast_float 性能
    {
        auto start = std::chrono::high_resolution_clock::now();

        for (int i = 0; i < ITERATIONS; ++i){
            for (size_t j = 0; j < NUM_STRINGS; ++j){
                double val;
                auto res = fast_float::from_chars(test_strings[j], test_strings[j] + std::strlen(test_strings[j]), val);
                if (res.ec == std::errc()){
                    sink += val * 0; // 同上,防止优化
                }
            }
        }

        auto end = std::chrono::high_resolution_clock::now();
        auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
        std::cout << "fast_float total time: " << ms << " ms\n";
    }

    return 0;
}

把源代码inclue目录下内容解压到当前目录。
编译和运行结果如下

g++ -std=c++20 -O3 -march=native fast_float_test.cpp -I .
./a.out
strtod total time: 363 ms
fast_float total time: 84 ms

注意,如果不想包含多个文件,fastfloat存储库还提供一个单独的头文件源代码, https://github.com/fastfloat/fast_float/releases/download/v8.0.2/fast_float.h 效果是一样的。
想要研究它的设计思想,可以查看论文

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值