Home / Forums / C++20 std::format vs snprintf & std::stringstream Benchmarks [Discussion #4]

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Tutorial

C++20 std::format vs snprintf & std::stringstream Benchmarks [Discussion #4]

OptiMax
Benchmark Specialist
MEMBER
Rep: 95
Join Date: Apr 2023
Posts: 6
Thanks: 31
4y ago · Mar 7, 2022 5:06 PM
#1
C++20 std::format combines the type-safety of std::stringstream with the performance of snprintf without needing manual buffer sizing.
OptiMax · Benchmark Specialist
BenchmarkDotNet, micro-optimizations, and zero-GC hot paths....
The following users thanked OptiMax for this post:
SpanSpecialist
Zero-Copy Memory
MEMBER
Rep: 273
Join Date: Aug 2021
Posts: 7
Thanks: 33
4y ago · Mar 7, 2022 7:25 PM
#2
std::format is significantly faster than iostreams because it does not allocate locale objects or virtual call overhead on every stream insertion.
SpanSpecialist · Zero-Copy Memory
Span<T>, ReadOnlySpan<T>, and stackalloc for fast substring ...
HexRays99
Senior Reverse Engineer
VIP
Rep: 380
Join Date: Apr 2022
Posts: 36
Thanks: 94
4y ago · Mar 8, 2022 9:25 PM
#3
In C++23, std::print also outputs directly to terminal stdout with UTF-8 support without formatting into an intermediate string first!
HexRays99 · Reverse Engineering & Static Analysis
CPP
// Always check your pointers!
if (!pLocalPlayer) return;