Skip to content

Instantly share code, notes, and snippets.

@peterjoel
Created May 3, 2020 15:03
Show Gist options
  • Save peterjoel/97431103893703e81926ae9bd8f768c5 to your computer and use it in GitHub Desktop.
Save peterjoel/97431103893703e81926ae9bd8f768c5 to your computer and use it in GitHub Desktop.
Benchmarking different methods of concatenating two strings
#![cfg(test)]
#![feature(test)]
extern crate test;
use test::bench::{black_box, Bencher};
fn concat_to_owned_push(a: &str, b: &str) -> String {
let mut s = a.to_owned();
s.push_str(b);
s
}
fn concat_to_owned_add(a: &str, b: &str) -> String {
a.to_owned() + b
}
fn concat_preallocate_push(a: &str, b: &str) -> String {
let mut s = String::with_capacity(a.len() + b.len());
s.push_str(a);
s.push_str(b);
s
}
fn concat_no_preallocate_push(a: &str, b: &str) -> String {
let mut s = String::new();
s.push_str(a);
s.push_str(b);
s
}
fn concat_no_alloc_push<'s>(s: &'s mut String, a: &str, b: &str) -> &'s str {
s.clear();
s.push_str(a);
s.push_str(b);
&*s
}
fn concat_format(a: &str, b: &str) -> String {
format!("{}{}", a, b)
}
#[bench]
fn bench_concat_to_owned_push(bench: &mut Bencher) {
let a = "lkjlkwejr";
let b = "kksdjfljijlaksd jlkasdj flkasdjf l";
bench.iter(|| black_box(concat_to_owned_push(a, b)));
}
#[bench]
fn bench_concat_to_owned_add(bench: &mut Bencher) {
let a = "lkjlkwejr";
let b = "kksdjfljijlaksd jlkasdj flkasdjf l";
bench.iter(|| black_box(concat_to_owned_add(a, b)));
}
#[bench]
fn bench_concat_preallocate_push(bench: &mut Bencher) {
let a = "lkjlkwejr";
let b = "kksdjfljijlaksd jlkasdj flkasdjf l";
bench.iter(|| black_box(concat_preallocate_push(a, b)));
}
#[bench]
fn bench_concat_no_preallocate_push(bench: &mut Bencher) {
let a = "lkjlkwejr";
let b = "kksdjfljijlaksd jlkasdj flkasdjf l";
bench.iter(|| black_box(concat_no_preallocate_push(a, b)));
}
#[bench]
fn bench_concat_no_alloc_push(bench: &mut Bencher) {
let a = "lkjlkwejr";
let b = "kksdjfljijlaksd jlkasdj flkasdjf l";
let mut buffer = String::with_capacity(a.len() + b.len());
let buf_ptr = buffer.as_mut_ptr();
let buf_len = buffer.len();
let buf_cap = buffer.capacity();
bench.iter(|| {
let mut buffer = unsafe { String::from_raw_parts(buf_ptr, buf_len, buf_cap) };
let result = concat_no_alloc_push(&mut buffer, a, b);
black_box(result);
std::mem::forget(buffer);
});
}
#[bench]
fn bench_concat_format(bench: &mut Bencher) {
let a = "lkjlkwejr";
let b = "kksdjfljijlaksd jlkasdj flkasdjf l";
bench.iter(|| black_box(concat_format(a, b)));
}
running 6 tests
test bench_concat_format ... bench: 256 ns/iter (+/- 24)
test bench_concat_no_alloc_push ... bench: 15 ns/iter (+/- 2)
test bench_concat_no_preallocate_push ... bench: 209 ns/iter (+/- 17)
test bench_concat_preallocate_push ... bench: 81 ns/iter (+/- 4)
test bench_concat_to_owned_add ... bench: 218 ns/iter (+/- 81)
test bench_concat_to_owned_push ... bench: 210 ns/iter (+/- 18)
test result: ok. 0 passed; 0 failed; 0 ignored; 6 measured; 0 filtered out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment