我想对Rust中的某些操作进行基准测试,但我似乎遇到了一些问题:
fn main(){
let needle = (0..100).map(|_| "b").collect::<String>();
let haystack = (0..100_000).map(|_| "a").collect::<String>();
println!("Data ready.");
for _ in 0..1_000_000 {
if haystack.contains( &needle ) {
// Stuff...
}
}
}
Run Code Online (Sandbox Code Playgroud)
上面需要很长时间才能完成,而Ruby中的相同操作在4.5秒左右完成:
needle = 'b' * 100
haystack = 'a' * 100_000
puts 'Data ready.'
1_000_000.times do
haystack.include? needle
end
Run Code Online (Sandbox Code Playgroud)
我不禁想到我做了一些根本错误的事情.在Rust中这样做的正确方法是什么?
rustc 1.0.0 (a59de37e9 2015-05-13) (built 2015-05-14)
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-linux]
Run Code Online (Sandbox Code Playgroud)