Create a gist now

Instantly share code, notes, and snippets.

@Measter /lib.rs
Last active Aug 8, 2017

What would you like to do?
Rust Perf Benchmark
#![feature(test)]
extern crate test;
#[cfg(test)]
mod tests {
use super::*;
use std::fs::{File};
use std::io::{BufWriter, Write, BufRead, BufReader};
#[bench]
fn io_write_unbuffered(b: &mut test::Bencher) {
let arr = ["Test".repeat(20), "Hello".repeat(20), "World".repeat(20)];
b.iter(|| {
let data = arr.iter().cycle();
let mut out = File::create("io_write_unbuffered.out").unwrap();
for line in data.take(50_000) {
writeln!(out, "{}", line).unwrap();
}
});
}
#[bench]
fn io_write_buffered(b: &mut test::Bencher) {
let arr = ["Test".repeat(20), "Hello".repeat(20), "World".repeat(20)];
b.iter(|| {
let data = arr.iter().cycle();
let out = File::create("io_write_buffered.out").unwrap();
let mut buf = BufWriter::new(out);
for line in data.take(50_000) {
writeln!(buf, "{}", line).unwrap();
}
});
}
#[bench]
fn io_read_lines(b: &mut test::Bencher) {
b.iter(|| {
let in_file = File::open("io_read_lines.in").unwrap();
let buf = BufReader::new(in_file);
for line in buf.lines() {
test::black_box(line).unwrap();
}
});
}
#[bench]
fn io_read_string(b: &mut test::Bencher) {
b.iter(|| {
let in_file = File::open("io_read_lines.in").unwrap();
let mut buf = BufReader::new(in_file);
let mut line = String::new();
while buf.read_line(&mut line).unwrap() > 0 {
test::black_box(&line);
line.clear();
}
});
}
#[bench]
fn loop_index(b: &mut test::Bencher) {
let arr = ["Test".repeat(20), "Hello".repeat(20), "World".repeat(20)];
let data: Vec<_> = arr.iter().cycle().take(100_000).collect();
b.iter(|| {
for i in 0..(data.len()) {
let x = data[i];
test::black_box(x);
}
});
}
#[bench]
fn loop_iter(b: &mut test::Bencher) {
let arr = ["Test".repeat(20), "Hello".repeat(20), "World".repeat(20)];
let data: Vec<_> = arr.iter().cycle().take(100_000).collect();
b.iter(|| {
for x in &data {
test::black_box(x);
}
});
}
#[bench]
fn iter_collect_needless(b: &mut test::Bencher) {
b.iter(|| {
let data: Vec<_> = (0..1_000_000).map(|i| test::black_box(i)).collect();
let data: Vec<_> = data.iter().filter(|i| test::black_box(**i > 30) ).collect();
test::black_box(data);
});
}
#[bench]
fn iter_collect_(b: &mut test::Bencher) {
b.iter(|| {
let data: Vec<_> = (0..1_000_000).map(|i| test::black_box(i)).filter(|i| test::black_box(*i > 30) ).collect();
test::black_box(data);
});
}
}
Following from compiler 1.18 stable.
Compiling perf_test v0.1.0 (file:///D:/Programming/Rust/RustProjects/perf_test)
Finished release [optimized] target(s) in 1.52 secs
Running target\release\deps\perf_test-a9916569d8306cc0.exe
running 8 tests
test tests::io_read_lines ... bench: 5,376,395 ns/iter (+/- 261,057)
test tests::io_read_string ... bench: 2,667,381 ns/iter (+/- 97,062)
test tests::io_write_buffered ... bench: 27,240,108 ns/iter (+/- 1,482,832)
test tests::io_write_unbuffered ... bench: 135,691,454 ns/iter (+/- 2,145,499)
test tests::iter_collect_ ... bench: 2,425,586 ns/iter (+/- 348,200)
test tests::iter_collect_needless ... bench: 5,088,152 ns/iter (+/- 425,462)
test tests::loop_index ... bench: 30,399 ns/iter (+/- 1,380)
test tests::loop_iter ... bench: 24,913 ns/iter (+/- 604)
test result: ok. 0 passed; 0 failed; 0 ignored; 8 measured; 0 filtered out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment