I was quite surprised when I found out that
pub fn conv() -> String {
'a'.to_string()
}
generates several times the instruction count of
pub fn conv() -> String {
"a".to_string()
}
(according to godbold).
Checking the (debug) build size of println!("{}", "a".to_string()); vs println!("{}", 'a'.to_string()); revealed the str variant being ~20kb lighter.
When doing some quick-and-dirty benchmarks, the str variant was more than 2x faster than the char-variant. benchmark code
test tests::bench_char_to_string ... bench: 39,890 ns/iter (+/- 42,078)
test tests::bench_str_to_string ... bench: 14,422 ns/iter (+/- 2,427)
Is this something that could be optimized?
I was quite surprised when I found out that
generates several times the instruction count of
(according to godbold).
Checking the (debug) build size of
println!("{}", "a".to_string());vsprintln!("{}", 'a'.to_string());revealed thestrvariant being ~20kb lighter.When doing some quick-and-dirty benchmarks, the
strvariant was more than 2x faster than the char-variant. benchmark codeIs this something that could be optimized?