Skip to content

Commit 2ea4578

Browse files
committed
---
yaml --- r: 83505 b: refs/heads/try c: 410a96c h: refs/heads/master i: 83503: 57527a7 v: v3
1 parent 7d95702 commit 2ea4578

File tree

2 files changed

+104
-1
lines changed

2 files changed

+104
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
refs/heads/master: 0e4d1fc8cae42e15e00f71d9f439b01bb25a86ae
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 6c08cc2db4f98e9f07ae7d50338396c4123c2f0a
5-
refs/heads/try: 2a9e7633043a79388fe3ed40c61a3961c2aedbcb
5+
refs/heads/try: 410a96cc79df4fbb3c2f30bdb2682b867df41a7e
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c

branches/try/src/libstd/c_str.rs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,3 +479,106 @@ mod tests {
479479
assert_eq!(c_str.as_str(), None);
480480
}
481481
}
482+
483+
#[cfg(test)]
484+
mod bench {
485+
use iter::range;
486+
use libc;
487+
use option::Some;
488+
use ptr;
489+
use extra::test::BenchHarness;
490+
491+
#[inline]
492+
fn check(s: &str, c_str: *libc::c_char) {
493+
do s.as_imm_buf |s_buf, s_len| {
494+
for i in range(0, s_len) {
495+
unsafe {
496+
assert_eq!(
497+
*ptr::offset(s_buf, i as int) as libc::c_char,
498+
*ptr::offset(c_str, i as int));
499+
}
500+
}
501+
}
502+
}
503+
504+
static s_short: &'static str = "Mary";
505+
static s_medium: &'static str = "Mary had a little lamb";
506+
static s_long: &'static str = "\
507+
Mary had a little lamb, Little lamb
508+
Mary had a little lamb, Little lamb
509+
Mary had a little lamb, Little lamb
510+
Mary had a little lamb, Little lamb
511+
Mary had a little lamb, Little lamb
512+
Mary had a little lamb, Little lamb";
513+
514+
fn bench_to_str(bh: &mut BenchHarness, s: &str) {
515+
do bh.iter {
516+
let c_str = s.to_c_str();
517+
do c_str.with_ref |c_str_buf| {
518+
check(s, c_str_buf)
519+
}
520+
}
521+
}
522+
523+
#[bench]
524+
fn bench_to_c_str_short(bh: &mut BenchHarness) {
525+
bench_to_str(bh, s_short)
526+
}
527+
528+
#[bench]
529+
fn bench_to_c_str_medium(bh: &mut BenchHarness) {
530+
bench_to_str(bh, s_medium)
531+
}
532+
533+
#[bench]
534+
fn bench_to_c_str_long(bh: &mut BenchHarness) {
535+
bench_to_str(bh, s_long)
536+
}
537+
538+
fn bench_to_c_str_unchecked(bh: &mut BenchHarness, s: &str) {
539+
do bh.iter {
540+
let c_str = unsafe { s.to_c_str_unchecked() };
541+
do c_str.with_ref |c_str_buf| {
542+
check(s, c_str_buf)
543+
}
544+
}
545+
}
546+
547+
#[bench]
548+
fn bench_to_c_str_unchecked_short(bh: &mut BenchHarness) {
549+
bench_to_c_str_unchecked(bh, s_short)
550+
}
551+
552+
#[bench]
553+
fn bench_to_c_str_unchecked_medium(bh: &mut BenchHarness) {
554+
bench_to_c_str_unchecked(bh, s_medium)
555+
}
556+
557+
#[bench]
558+
fn bench_to_c_str_unchecked_long(bh: &mut BenchHarness) {
559+
bench_to_c_str_unchecked(bh, s_long)
560+
}
561+
562+
fn bench_with_c_str(bh: &mut BenchHarness, s: &str) {
563+
do bh.iter {
564+
do s.with_c_str |c_str_buf| {
565+
check(s, c_str_buf)
566+
}
567+
}
568+
}
569+
570+
#[bench]
571+
fn bench_with_c_str_short(bh: &mut BenchHarness) {
572+
bench_with_c_str(bh, s_short)
573+
}
574+
575+
#[bench]
576+
fn bench_with_c_str_medium(bh: &mut BenchHarness) {
577+
bench_with_c_str(bh, s_medium)
578+
}
579+
580+
#[bench]
581+
fn bench_with_c_str_long(bh: &mut BenchHarness) {
582+
bench_with_c_str(bh, s_long)
583+
}
584+
}

0 commit comments

Comments
 (0)