-
-
Save twittner/82dd2c83a3524b9ad2c84918183d008c to your computer and use it in GitHub Desktop.
Vec<u8>/Bytes cloning benchmark
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
[package] | |
name = "bench" | |
version = "0.1.0" | |
edition = "2018" | |
[dev-dependencies] | |
bytes = "0.5" | |
criterion = "0.3" | |
[[bench]] | |
name = "clone" | |
harness = false |
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
use bytes::{Bytes, BytesMut}; | |
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; | |
criterion_group!(benches, clone); | |
criterion_main!(benches); | |
fn clone(c: &mut Criterion) { | |
let mut group = c.benchmark_group("10 clones of Vec<u8>"); | |
for v in &[vec(1), vec(10), vec(100), vec(1000), vec(10_000)] { | |
group.bench_with_input(BenchmarkId::from_parameter(v.len()), v, |b, v| { | |
b.iter(|| { | |
for _ in 0 .. 10 { | |
let _ = v.clone(); | |
} | |
}) | |
}); | |
} | |
group.finish(); | |
let mut group = c.benchmark_group("10 clones of Bytes"); | |
for v in &[bytes(1), bytes(10), bytes(100), bytes(1000), bytes(10_000)] { | |
group.bench_with_input(BenchmarkId::from_parameter(v.len()), v, |b, v| { | |
b.iter(|| { | |
for _ in 0 .. 10 { | |
let _ = v.clone(); | |
} | |
}) | |
}); | |
} | |
group.finish(); | |
let mut group = c.benchmark_group("64 bytes long Vec<u8> cloning"); | |
for v in &[1, 10, 100, 1000, 10_000] { | |
group.bench_with_input(BenchmarkId::from_parameter(v), v, |b, &v| { | |
let data = vec(64); | |
b.iter(|| { | |
for _ in 0 .. v { | |
let _ = data.clone(); | |
} | |
}) | |
}); | |
} | |
group.finish(); | |
let mut group = c.benchmark_group("64 bytes long Bytes cloning"); | |
for v in &[1, 10, 100, 1000, 10_000] { | |
group.bench_with_input(BenchmarkId::from_parameter(v), v, |b, &v| { | |
let data = bytes(64); | |
b.iter(|| { | |
for _ in 0 .. v { | |
let _ = data.clone(); | |
} | |
}) | |
}); | |
} | |
group.finish(); | |
} | |
fn vec(n: usize) -> Vec<u8> { | |
let mut data = Vec::with_capacity(n); | |
data.resize(n, 0); | |
data | |
} | |
fn bytes(n: usize) -> Bytes { | |
let mut data = BytesMut::with_capacity(n); | |
data.resize(n, 0); | |
data.freeze() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment