Hi all,
I'm currently benching some ideas on building up a url with params. This is something that'll be called often in my library, but isn't really cachable.
So far I've got these two examples:
#[bench]
fn format_url_macro(b: &mut Bencher) {
let index = "test_idx".to_string();
let name = "test_alias".to_string();
b.iter(|| {
let _ = format!("/{}/_alias/{}", index, name);
});
}
#[bench]
fn format_url_concat(b: &mut Bencher) {
let index = "test_idx".to_string();
let name = "test_alias".to_string();
b.iter(|| {
let mut url = "/".to_string();
url = url + &index[..] + "/_alias/" + &name[..];
});
}
With these results on my machine:
test format_url_concat ... bench: 180 ns/iter (+/- 56)
test format_url_macro ... bench: 192 ns/iter (+/- 10)
Does anyone have any other ideas I could try to build a string efficiently? (Also not sure why the concat one has such crazy variance?)
Because this stuff is codegenned, and is internal, it doesn't matter if it's ugly.