[英]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.