繁体   English   中英

零成本抽象:for-loop与迭代器的性能

[英]Zero-cost abstractions: performance of for-loop vs. iterators

阅读零成本抽象并查看生锈简介:一种具有高级抽象的低级语言我试图比较两种计算向量点积的方法:一种使用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而不是slice给出以下结果。

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 case并使用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))
}

这对我来说似乎是零成本。 我稍微更加惯用地编写了代码,对两个测试使用相同的随机值,然后多次测试:

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];
    }
    result
}

fn dot_product_2(x: &[f64], y: &[f64]) -> f64 {
    x.iter().zip(y).map(|(&a, &b)| a * b).sum()
}
fn rand_array(cnt: usize) -> Vec<f64> {
    let mut rng = rand::rngs::StdRng::seed_from_u64(42);
    rng.sample_iter(&rand::distributions::Standard).take(cnt).collect()
}

#[bench]
fn bench_small_1(b: &mut Bencher) {
    let samples = rand_array(2 * LEN);
    let (s0, s1) = samples.split_at(LEN);
    b.iter(|| dot_product_1(s0, s1))
}

#[bench]
fn bench_small_2(b: &mut Bencher) {
    let samples = rand_array(2 * LEN);
    let (s0, s1) = samples.split_at(LEN);
    b.iter(|| dot_product_2(s0, s1))
}
bench_small_1   20 ns/iter (+/- 6)
bench_small_2   17 ns/iter (+/- 1)

bench_small_1   19 ns/iter (+/- 3)
bench_small_2   17 ns/iter (+/- 2)

bench_small_1   19 ns/iter (+/- 5)
bench_small_2   17 ns/iter (+/- 3)

bench_small_1   19 ns/iter (+/- 3)
bench_small_2   24 ns/iter (+/- 7)

bench_small_1   28 ns/iter (+/- 1)
bench_small_2   24 ns/iter (+/- 1)

bench_small_1   27 ns/iter (+/- 1)
bench_small_2   25 ns/iter (+/- 1)

bench_small_1   28 ns/iter (+/- 1)
bench_small_2   25 ns/iter (+/- 1)

bench_small_1   28 ns/iter (+/- 1)
bench_small_2   25 ns/iter (+/- 1)

bench_small_1   28 ns/iter (+/- 0)
bench_small_2   25 ns/iter (+/- 1)

bench_small_1   28 ns/iter (+/- 1)
bench_small_2   17 ns/iter (+/- 1)

在10次运行中的9次中,惯用代码比for循环更快。 这是在具有32 GB RAM的2.9 GHz Core i9(I9-8950HK)上完成的,使用rustc 1.31.0-nightly (fc403ad98 2018-09-30)编译rustc 1.31.0-nightly (fc403ad98 2018-09-30)

为了好玩,我重新编写了使用标准的基准测试,这是Haskell基准测试库的一个端口。

Cargo.toml

[package]
name = "mats-zero-cost-rust"
version = "0.1.0"
authors = ["mats"]

[dev-dependencies]
criterion = "0.2"
rand = "0.4"

[[bench]]
name = "my_benchmark"
harness = false

长凳/ my_benchmark.rs

#[macro_use]
extern crate criterion;
extern crate rand;

use std::cmp::min;

use criterion::Criterion;

use rand::{thread_rng, Rng};

const LEN: usize = 30;

fn dot_product_loop(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_iter(x: &[f64], y: &[f64]) -> f64 {
    x.iter().zip(y).map(|(&a, &b)| a * b).sum()
}

fn rand_array(cnt: u32) -> Vec<f64> {
    let mut rng = thread_rng();
    (0..cnt).map(|_| rng.gen()).collect()
}

fn criterion_loop_with_slice(c: &mut Criterion) {
    c.bench_function("loop with slice", |b| {
        let samples = rand_array(2 * LEN as u32);
        b.iter(|| dot_product_loop(&samples[0..LEN], &samples[LEN..2 * LEN]))
    });
}

fn criterion_loop_without_slice(c: &mut Criterion) {
    c.bench_function("loop without slice", |b| {
        let samples = rand_array(2 * LEN as u32);
        let (s0, s1) = samples.split_at(LEN);
        b.iter(|| dot_product_loop(s0, s1))
    });
}

fn criterion_iter_with_slice(c: &mut Criterion) {
    c.bench_function("iterators with slice", |b| {
        let samples = rand_array(2 * LEN as u32);
        b.iter(|| dot_product_iter(&samples[0..LEN], &samples[LEN..2 * LEN]))
    });
}

fn criterion_iter_without_slice(c: &mut Criterion) {
    c.bench_function("iterators without slice", |b| {
        let samples = rand_array(2 * LEN as u32);
        let (s0, s1) = samples.split_at(LEN);
        b.iter(|| dot_product_iter(s0, s1))
    });
}

criterion_group!(benches, criterion_loop_with_slice, criterion_loop_without_slice, criterion_iter_with_slice, criterion_iter_without_slice);
criterion_main!(benches);

我观察到这些结果;

kolmodin@blin:~/code/mats-zero-cost-rust$ cargo bench
   Compiling mats-zero-cost-rust v0.1.0 (/home/kolmodin/code/mats-zero-cost-rust)                                                                                                                                  
    Finished release [optimized] target(s) in 1.16s                                                                                                                                                                
     Running target/release/deps/my_benchmark-6f00e042fd40bc1d
Gnuplot not found, disabling plotting
loop with slice         time:   [7.5794 ns 7.6843 ns 7.8016 ns]                             
Found 14 outliers among 100 measurements (14.00%)
  1 (1.00%) high mild
  13 (13.00%) high severe

loop without slice      time:   [24.384 ns 24.486 ns 24.589 ns]                                
Found 3 outliers among 100 measurements (3.00%)
  2 (2.00%) low severe
  1 (1.00%) low mild

iterators with slice    time:   [13.842 ns 13.852 ns 13.863 ns]                                  
Found 6 outliers among 100 measurements (6.00%)
  1 (1.00%) low mild
  4 (4.00%) high mild
  1 (1.00%) high severe

iterators without slice time:   [13.420 ns 13.424 ns 13.430 ns]                                     
Found 12 outliers among 100 measurements (12.00%)
  1 (1.00%) low mild
  1 (1.00%) high mild
  10 (10.00%) high severe

Gnuplot not found, disabling plotting

在AMD Ryzen 7 2700X上使用rustc 1.30.0 (da5f414c2 2018-10-24)

迭代器实现获得了使用slicesplit_at类似结果。

循环实现获得了非常不同的结果。 带切片的版本明显更快。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM