Last active
April 16, 2018 16:12
-
-
Save ryochack/7772dc38bde3a2a62cbe91820fc1f887 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(test)] | |
extern crate test; | |
#[cfg(test)] | |
mod tests { | |
use super::*; | |
use std::str; | |
use test::Bencher; | |
#[bench] | |
fn bench_bytes_to_str(b: &mut Bencher) { | |
let contents = "sample message"; | |
b.iter(|| contents.starts_with(str::from_utf8(&[b's', b'a', b'm']).unwrap())); | |
} | |
#[bench] | |
fn bench_str_to_bytes(b: &mut Bencher) { | |
let contents = "sample message"; | |
b.iter(|| contents.as_bytes().starts_with(&[b's', b'a', b'm'])); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
rustc 1.27.0-nightly (bd40cbbe1 2018-04-14)
strをbytesに変換する方が14倍速い。(as_bytes()内部でやってるのはunsafeでのキャストのみなのでコストが小さい)