阅读零成本抽象和观看介绍Rust:带有高级抽象的低级语言,我尝试比较计算向量点积的两种方法:一种使用for循环,另一种使用迭代器。
#![feature(test)]
extern crate rand;
extern crate test;
use std::cmp::min;
fn dot_product_1(x: &[f64], y: &[f64]) -> f64 {
let mut result: f64 = 0.0;
for i in 0..min(x.len(), y.len()) {
result += x[i] * y[i];
}
return result;
}
fn dot_product_2(x: &[f64], y: &[f64]) -> f64 {
x.iter().zip(y).map(|(&a, &b)| a * b).sum::<f64>()
}
#[cfg(test)]
mod bench {
use test::Bencher;
use rand::{Rng,thread_rng};
use super::*;
const LEN: usize = 30;
#[test]
fn test_1() {
let x = [1.0, 2.0, 3.0];
let y = [2.0, 4.0, 6.0];
let result = dot_product_1(&x, &y);
assert_eq!(result, 28.0);
}
#[test]
fn test_2() {
let x = [1.0, 2.0, 3.0];
let y = [2.0, 4.0, 6.0];
let result = dot_product_2(&x, &y);
assert_eq!(result, 28.0);
}
fn rand_array(cnt: u32) -> Vec<f64> {
let mut rng = thread_rng();
(0..cnt).map(|_| rng.gen::<f64>()).collect()
}
#[bench]
fn bench_small_1(b: &mut Bencher) {
let samples = rand_array(2*LEN as u32);
b.iter(|| {
dot_product_1(&samples[0..LEN], &samples[LEN..2*LEN])
})
}
#[bench]
fn bench_small_2(b: &mut Bencher) {
let samples = rand_array(2*LEN as u32);
b.iter(|| {
dot_product_2(&samples[0..LEN], &samples[LEN..2*LEN])
})
}
}
上述链接中后面的那个声称,带有迭代器的版本应该具有类似的性能“实际上会更快一点”。然而,当对这两者进行基准测试时,我得到了非常不同的结果:
running 2 tests
test bench::bench_small_loop ... bench: 20 ns/iter (+/- 1)
test bench::bench_small_iter ... bench: 24 ns/iter (+/- 2)
test result: ok. 0 passed; 0 failed; 0 ignored; 2 measured; 0 filtered out
那么,“零成本抽象”去哪了?
更新: 添加了@wimh提供的foldr
示例,并使用split_at
代替切片,得到以下结果。
running 3 tests
test bench::bench_small_fold ... bench: 18 ns/iter (+/- 1)
test bench::bench_small_iter ... bench: 21 ns/iter (+/- 1)
test bench::bench_small_loop ... bench: 24 ns/iter (+/- 1)
test result: ok. 0 passed; 0 failed; 0 ignored; 3 measured; 0 filtered out
看起来额外的时间直接或间接地来自于在测量代码内部构建这些片段。为了确认这确实是情况,我尝试了以下两种方法,结果相同(以下示例展示了foldr
案例和使用map
+ sum
):
因此,看起来额外的时间是由测量代码内部构建这些片段所导致的,无论是直接还是间接的。为了验证这个情况,我尝试了以下两种方法,结果相同(下面是foldr
的案例和使用map
+ sum
的示例)。
#[bench]
fn bench_small_iter(b: &mut Bencher) {
let samples = rand_array(2 * LEN);
let s0 = &samples[0..LEN];
let s1 = &samples[LEN..2 * LEN];
b.iter(|| dot_product_iter(s0, s1))
}
#[bench]
fn bench_small_fold(b: &mut Bencher) {
let samples = rand_array(2 * LEN);
let (s0, s1) = samples.split_at(LEN);
b.iter(|| dot_product_fold(s0, s1))
}
dot_product_iter
调用末尾的分号应该被移除,否则编译器可能会决定完全消除它(虽然它似乎没有这样做)。 - David BrownLEN
的增加,我发现运行时间越来越接近,如果我使两个函数都#[inline(never)]
,那么循环变量始终比较慢(内联版本可能可以避免循环边界检查)。 - David BrownLEN
时,iter 获得了净优势。 - Stargateur