Skip to content

Instantly share code, notes, and snippets.

@trishume
Created July 5, 2017 15:53
Show Gist options
  • Save trishume/aca4083cdbf237f04709633dbe55b643 to your computer and use it in GitHub Desktop.
Save trishume/aca4083cdbf237f04709633dbe55b643 to your computer and use it in GitHub Desktop.
Faster Rust benchmark that still gives no information
pub fn suffix_vec_ref(s: &str) -> Vec<&str> {
let mut vec = Vec::with_capacity(s.len());
let mut slice = &s[1..];
while !slice.is_empty() {
vec.push(slice.clone());
slice = &slice[1..];
}
vec
}
use std::str::Chars;
pub struct Tails<'a>(Chars<'a>);
impl<'a> Iterator for Tails<'a> {
type Item = Chars<'a>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
let old = self.0.clone();
if let None = self.0.next() {
None
} else {
Some(old)
}
}
}
pub fn suffix_full_lazy<'a>(s: &'a str) -> Tails<'a> {
let mut iter = s.chars();
iter.next();
Tails(iter)
}

Before all changes I made

test test::bench_big_fib               ... bench:         153 ns/iter (+/- 22)
test test::bench_iterative_fib         ... bench:         148 ns/iter (+/- 7)
test test::bench_suffix                ... bench:         149 ns/iter (+/- 19)
test test::bench_suffix_extra_long     ... bench:      62,122 ns/iter (+/- 7,134)
test test::bench_suffix_long           ... bench:       3,445 ns/iter (+/- 583)
test test::bench_suffix_medium         ... bench:         721 ns/iter (+/- 253)
test test::bench_suffix_medium_unicode ... bench:         806 ns/iter (+/- 162)
test test::bench_suffix_ref            ... bench:          47 ns/iter (+/- 4)

After all changes I made:

running 7 tests
test test::bench_suffix                ... bench:         135 ns/iter (+/- 7)
test test::bench_suffix_extra_long     ... bench:      59,130 ns/iter (+/- 14,078)
test test::bench_suffix_full_lazy      ... bench:          14 ns/iter (+/- 5)
test test::bench_suffix_long           ... bench:       2,884 ns/iter (+/- 1,152)
test test::bench_suffix_medium         ... bench:         593 ns/iter (+/- 72)
test test::bench_suffix_medium_unicode ... bench:         691 ns/iter (+/- 153)
test test::bench_suffix_ref            ... bench:          29 ns/iter (+/- 11)
#[bench]
fn bench_suffix_full_lazy(b: &mut Bencher) {
let s = "tails".to_string();
b.iter(|| {
for cs in suffix_full_lazy(&s) {
for c in cs {
test::black_box(c);
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment