Good point! I've added the following two benches using .concat()
. One with a vec
and the other with an array
:
#[bench]
fn format_url_vec_concat(b: &mut Bencher) {
let index = "test_idx".to_string();
let name = "test_alias".to_string();
b.iter(|| {
let url = vec![ "/", &index[..], "/_alias/", &name[..] ].concat();
url
});
}
#[bench]
fn format_url_array_concat(b: &mut Bencher) {
let index = "test_idx".to_string();
let name = "test_alias".to_string();
b.iter(|| {
let url = [ "/", &index[..], "/_alias/", &name[..] ].concat();
url
});
}
Results on my machine look like:
test format_url_array_concat ... bench: 69 ns/iter (+/- 16)
test format_url_concat ... bench: 280 ns/iter (+/- 59)
test format_url_macro ... bench: 287 ns/iter (+/- 36)
test format_url_push ... bench: 51 ns/iter (+/- 7)
test format_url_vec_concat ... bench: 109 ns/iter (+/- 4)
test format_url_write ... bench: 83 ns/iter (+/- 30)
So the array concat is only a fraction slower than the push_str
method and the vec
method is a bit slower than the write
one.
(Note, I'm also getting crazy variance in the benches on Windows... Some runs show the figures above, some look more like this:
test format_url_array_concat ... bench: 181 ns/iter (+/- 7)
test format_url_concat ... bench: 708 ns/iter (+/- 21)
test format_url_macro ... bench: 734 ns/iter (+/- 40)
test format_url_push ... bench: 129 ns/iter (+/- 4)
test format_url_vec_concat ... bench: 110 ns/iter (+/- 148)
test format_url_write ... bench: 197 ns/iter (+/- 4)
Not sure why, but taking the variance into account the url_push
still looks the fastest.