Skip to content

Commit e5cbede

Browse files
committed
std: add preliminary str benchmark.
1 parent 786318f commit e5cbede

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/libstd/str.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3532,3 +3532,48 @@ mod tests {
35323532
assert_eq!(5, sum_len([s.as_slice()]));
35333533
}
35343534
}
3535+
3536+
#[cfg(test)]
3537+
mod bench {
3538+
use extra::test::BenchHarness;
3539+
use str;
3540+
3541+
#[bench]
3542+
fn is_utf8_100_ascii(bh: &mut BenchHarness) {
3543+
3544+
let s = bytes!("Hello there, the quick brown fox jumped over the lazy dog! \
3545+
Lorem ipsum dolor sit amet, consectetur. ");
3546+
3547+
assert_eq!(100, s.len());
3548+
do bh.iter {
3549+
str::is_utf8(s);
3550+
}
3551+
}
3552+
3553+
#[bench]
3554+
fn is_utf8_100_multibyte(bh: &mut BenchHarness) {
3555+
let s = bytes!("𐌀𐌖𐌋𐌄𐌑𐌉ปรدولة الكويتทศไทย中华𐍅𐌿𐌻𐍆𐌹𐌻𐌰");
3556+
assert_eq!(100, s.len());
3557+
do bh.iter {
3558+
str::is_utf8(s);
3559+
}
3560+
}
3561+
3562+
#[bench]
3563+
fn map_chars_100_ascii(bh: &mut BenchHarness) {
3564+
let s = "HelloHelloHelloHelloHelloHelloHelloHelloHelloHello\
3565+
HelloHelloHelloHelloHelloHelloHelloHelloHelloHello";
3566+
do bh.iter {
3567+
s.map_chars(|c| ((c as uint) + 1) as char);
3568+
}
3569+
}
3570+
3571+
#[bench]
3572+
fn map_chars_100_multibytes(bh: &mut BenchHarness) {
3573+
let s = "𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑\
3574+
𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑𐌀𐌖𐌋𐌄𐌑";
3575+
do bh.iter {
3576+
s.map_chars(|c| ((c as uint) + 1) as char);
3577+
}
3578+
}
3579+
}

0 commit comments

Comments
 (0)