简体   繁体   English

为什么 {fmt} 比 std::stringstream 慢?

[英]Why is {fmt} slower than std::stringstream?

From what I read online the fmt library is supposed to be incredibly much faster than eg stringstreams.从我在线阅读的内容来看,fmt 库应该比字符串流快得多。

However, I did some simple benchmarking (measuring system time, see code below) and it seems that fmt is always slower than eg stringstreams.然而,我做了一些简单的基准测试(测量系统时间,见下面的代码),似乎 fmt 总是比字符串流慢。 Am I getting something wrong?我有什么问题吗?

uint64_t start;
uint64_t stop;

long MAXCOUNT = 10000000;

std::srand(123);

int* numbers =  new int[MAXCOUNT];
for ( int i = 0; i < MAXCOUNT; i++) {
    numbers[i] = std::rand();
}

{
    std::string result;
    start = currentTimeInMillis();
    for ( int i = 0; i < MAXCOUNT; i++) {
        result += fmt::format("Number {} is great!", numbers[i]);
    }
    stop = currentTimeInMillis();
    fmt::print("timing fmt : {} ms   /   string length: {}\n", stop-start, result.size());
}


{
    std::string result;
    std::stringstream ss;
    start = currentTimeInMillis();
    for ( int i = 0; i < MAXCOUNT; i++) {
        ss << "Number " << numbers[i] << " is great!";
    }
    result = ss.str();
    stop = currentTimeInMillis();
    fmt::print("timing stds: {} ms   /   string length: {}\n", stop-start, result.size());
}

A typical result (optimization level O3 - less is even worse) of this code is:此代码的典型结果(优化级别 O3 - 更少甚至更糟)是:

timing fmt : 1414 ms   /   string length: 264823200
timing stds: 1287 ms   /   string length: 264823200

First, I don't get the same numbers on my machine, for me fmt is faster:首先,我的机器上没有相同的数字,对我来说fmt更快:

timing fmt : 1713 ms   /   string length: 264825935
timing stds: 2483 ms   /   string length: 264825935

Second, to get a fair comparison, replace其次,为了得到公平的比较,替换

result += fmt::format("Number {} is great!", numbers[i]);

with

fmt::format_to(std::back_inserter(result), "Number {} is great!", numbers[i]);

This improves the timing to (on my machine again)这提高了时间(再次在我的机器上)

timing fmt : 1153 ms   /   string length: 264825935
timing stds: 2482 ms   /   string length: 264825935

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么std :: fill(0)比std :: fill(1)慢? - Why is std::fill(0) slower than std::fill(1)? 为什么stoi比没有-O3的stringstream慢得多? - Why stoi is much slower than stringstream without -O3? 为什么std :: sin()和std :: cos()比sin()和cos()慢? - Why is std::sin() and std::cos() slower than sin() and cos()? 为什么std :: shuffle比std :: sort慢(甚至慢)? - Why is std::shuffle as slow (or even slower than) std::sort? 为什么`std :: stringstream :: stringstream(std :: string &&)`不存在? - Why doesn't `std::stringstream::stringstream(std::string&&)` exist? c ++为什么std :: async比顺序执行慢 - c++ why std::async slower than sequential execution 为什么这个普通数组实现比std :: vector实现性能慢? - Why is this plain array implementation slower than the std::vector implementation performance? 为什么我的set_intersection代码比标准慢? - Why is my code for set_intersection slower than the std? 为什么我的swap <string,string>比std版本慢得多? - Why is my swap<string,string> far slower than the std version? 为什么全局范围内的sqrt比MinGW中的std :: sqrt慢得多? - Why sqrt in global scope is much slower than std::sqrt in MinGW?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM