diff options
| author | Erick Tryzelaar <erick.tryzelaar@gmail.com> | 2013-09-18 12:32:45 -0700 |
|---|---|---|
| committer | Erick Tryzelaar <erick.tryzelaar@gmail.com> | 2013-09-26 22:20:40 -0700 |
| commit | 410a96cc79df4fbb3c2f30bdb2682b867df41a7e (patch) | |
| tree | 4cc4111c93baf937e20bdc9993b4ad79da8415bc /src/libstd | |
| parent | 2a9e7633043a79388fe3ed40c61a3961c2aedbcb (diff) | |
| download | rust-410a96cc79df4fbb3c2f30bdb2682b867df41a7e.tar.gz rust-410a96cc79df4fbb3c2f30bdb2682b867df41a7e.zip | |
std: Add benchmarks to c_str
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/c_str.rs | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/src/libstd/c_str.rs b/src/libstd/c_str.rs index 82726627bc8..3506faadd3e 100644 --- a/src/libstd/c_str.rs +++ b/src/libstd/c_str.rs @@ -479,3 +479,106 @@ mod tests { assert_eq!(c_str.as_str(), None); } } + +#[cfg(test)] +mod bench { + use iter::range; + use libc; + use option::Some; + use ptr; + use extra::test::BenchHarness; + + #[inline] + fn check(s: &str, c_str: *libc::c_char) { + do s.as_imm_buf |s_buf, s_len| { + for i in range(0, s_len) { + unsafe { + assert_eq!( + *ptr::offset(s_buf, i as int) as libc::c_char, + *ptr::offset(c_str, i as int)); + } + } + } + } + + static s_short: &'static str = "Mary"; + static s_medium: &'static str = "Mary had a little lamb"; + static s_long: &'static str = "\ + Mary had a little lamb, Little lamb + Mary had a little lamb, Little lamb + Mary had a little lamb, Little lamb + Mary had a little lamb, Little lamb + Mary had a little lamb, Little lamb + Mary had a little lamb, Little lamb"; + + fn bench_to_str(bh: &mut BenchHarness, s: &str) { + do bh.iter { + let c_str = s.to_c_str(); + do c_str.with_ref |c_str_buf| { + check(s, c_str_buf) + } + } + } + + #[bench] + fn bench_to_c_str_short(bh: &mut BenchHarness) { + bench_to_str(bh, s_short) + } + + #[bench] + fn bench_to_c_str_medium(bh: &mut BenchHarness) { + bench_to_str(bh, s_medium) + } + + #[bench] + fn bench_to_c_str_long(bh: &mut BenchHarness) { + bench_to_str(bh, s_long) + } + + fn bench_to_c_str_unchecked(bh: &mut BenchHarness, s: &str) { + do bh.iter { + let c_str = unsafe { s.to_c_str_unchecked() }; + do c_str.with_ref |c_str_buf| { + check(s, c_str_buf) + } + } + } + + #[bench] + fn bench_to_c_str_unchecked_short(bh: &mut BenchHarness) { + bench_to_c_str_unchecked(bh, s_short) + } + + #[bench] + fn bench_to_c_str_unchecked_medium(bh: &mut BenchHarness) { + bench_to_c_str_unchecked(bh, s_medium) + } + + #[bench] + fn bench_to_c_str_unchecked_long(bh: &mut BenchHarness) { + bench_to_c_str_unchecked(bh, s_long) + } + + fn bench_with_c_str(bh: &mut BenchHarness, s: &str) { + do bh.iter { + do s.with_c_str |c_str_buf| { + check(s, c_str_buf) + } + } + } + + #[bench] + fn bench_with_c_str_short(bh: &mut BenchHarness) { + bench_with_c_str(bh, s_short) + } + + #[bench] + fn bench_with_c_str_medium(bh: &mut BenchHarness) { + bench_with_c_str(bh, s_medium) + } + + #[bench] + fn bench_with_c_str_long(bh: &mut BenchHarness) { + bench_with_c_str(bh, s_long) + } +} |
