void example() {
for (int64_t i = 1; i <= 20; ++i) {
for (int64_t j = 1; j <= 1'000'000; ++j) {
std::to_string(j);
}
}
}
runs with GCC in 2.0s and with clang in 133ms, so 15x faster.I've also benchmarked it in Julia:
function example()
for j = 1:20, i = 1:1_000_000
string(i)
end
end
which runs in 592ms. Julia has no small string optimization and does have proper unicode support by default.None of the compilers can see that the loop can be optimized out.