Skip to content

Instantly share code, notes, and snippets.

@alexcrichton
Created December 13, 2014 00:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexcrichton/162a5f8f93062800c914 to your computer and use it in GitHub Desktop.
Save alexcrichton/162a5f8f93062800c914 to your computer and use it in GitHub Desktop.
extern crate test;
const TEXT: &'static str = "Hello World!";
const LONG_TEXT: &'static str = "
There are not many persons who know what wonders are opened to them in the
stories and visions of their youth; for when as children we listen and dream,
we think but half-formed thoughts, and when as men we try to remember, we are
dulled and prosaic with the poison of life. But some of us awake in the night
with strange phantasms of enchanted hills and gardens, of fountains that sing
in the sun, of golden cliffs overhanging murmuring seas, of plains that stretch
down to sleeping cities of bronze and stone, and of shadowy companies of heroes
that ride caparisoned white horses along the edges of thick forests; and then
we know that we have looked back through the ivory gates into that world of
wonder which was ours before we were wise and unhappy.";
fn bench_direct(b: &mut test::Bencher, string: &str) {
b.iter(|| {
test::black_box(string.into_string());
});
b.bytes = string.as_bytes().len().to_u64().unwrap();
}
fn bench_via_format(b: &mut test::Bencher, string: &str) {
b.iter(|| {
test::black_box(string.to_string());
});
b.bytes = string.as_bytes().len().to_u64().unwrap();
}
fn very_long_string() -> String {
let mut s = String::with_capacity(LONG_TEXT.len() * 1000);
for _ in range(0u32, 1000) {
s.push_str(LONG_TEXT);
}
s
}
#[bench] fn bench_short_direct(b: &mut test::Bencher) { bench_direct(b, TEXT); }
#[bench] fn bench_short_via_format(b: &mut test::Bencher) { bench_via_format(b, TEXT); }
#[bench] fn bench_medium_direct(b: &mut test::Bencher) { bench_direct(b, LONG_TEXT); }
#[bench] fn bench_medium_via_format(b: &mut test::Bencher) { bench_via_format(b, LONG_TEXT); }
#[bench] fn bench_long_direct(b: &mut test::Bencher) { bench_direct(b, very_long_string().as_slice()); }
#[bench] fn bench_long_via_format(b: &mut test::Bencher) { bench_via_format(b, very_long_string().as_slice()); }
running 6 tests
test bench_long_direct ... bench: 191766 ns/iter (+/- 4034) = 3941 MB/s
test bench_long_via_format ... bench: 586749 ns/iter (+/- 12841) = 1288 MB/s
test bench_medium_direct ... bench: 37 ns/iter (+/- 2) = 20432 MB/s
test bench_medium_via_format ... bench: 664 ns/iter (+/- 19) = 1138 MB/s
test bench_short_direct ... bench: 20 ns/iter (+/- 0) = 600 MB/s
test bench_short_via_format ... bench: 65 ns/iter (+/- 2) = 184 MB/s
test result: ok. 0 passed; 0 failed; 0 ignored; 6 measured
Samples: 61K of event 'cycles', Event count (approx.): 58774277955
52.06% foo [kernel.kallsyms] [k] 0xffffffff8104f45a
27.65% foo foo [.] str::is_utf8::h6996a9c8a713fcb5Z8r
12.31% foo foo [.] bench_direct::closure.1422
2.76% foo foo [.] vec::Vec$LT$u8$GT$.fmt..FormatWriter::write::h3
2.24% foo foo [.] je_sdallocx
1.06% foo foo [.] je_mallocx
0.29% foo foo [.] vec::Vec$LT$T$GT$::reserve::h593269131940607514
0.29% foo foo [.] very_long_string::h3f95259094d8730eBba
0.27% foo foo [.] fmt::write::he5559dc0465fcb0cerA
0.26% foo foo [.] bench_via_format::closure.1848
0.20% foo foo [.] fmt::Formatter$LT$$x27a$GT$::run::h8d9aa2ee4516
0.10% foo foo [.] fmt::str.Show::fmt::h410e1aecf309504aUJA
0.09% foo foo [.] fmt::Formatter$LT$$x27a$GT$::pad::h7fd3397e8a51
0.09% foo foo [.] arena_purge_stashed
0.08% foo foo [.] Bencher::iter::closure.1391
0.07% foo foo [.] fmt::_$BP$$x27a$x20T.Show::fmt::h65382186988000
0.03% foo libc-2.19.so [.] __madvise
0.02% foo libpthread-2.19.so [.] pthread_mutex_lock
0.02% foo foo [.] vec::alloc_or_realloc::h4819189490705178488
0.02% foo libpthread-2.19.so [.] pthread_mutex_unlock
0.01% foo foo [.] arena_avail_tree_remove
0.01% foo foo [.] je_pages_purge
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment