Efficiency of `println!` and `format!`

Almost certainly. The formatting infrastructure is an optimization barrier, so it is unlikely that the compiler will be able to see through the intermediate String allocation.

Note that you can also use format_args! to create an intermediate object without allocations, if the circumstances allow this.

In general, the formatting infrastructure is not very performant, so if it shows up in profiles, I suggest making use of String::with_capacity and String::push_str to avoid it. To format integers/floats, the itoa and dtoa crates provide much faster alternatives than the default formatting methods. ufmt is also something to look at if you need more performance.

9 Likes