Skip to content

Commit

Permalink
benchmarks against flexpolyline
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelkirk committed May 9, 2024
1 parent 5340961 commit a3a4028
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ geo-types = "0.7.8"
[dev-dependencies]
rand = "0.8.5"
criterion = "0.5.1"
flexpolyline = "0.1.0"

[[bench]]
name = "benchmarks"
Expand Down
27 changes: 26 additions & 1 deletion benches/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ fn build_coords() -> Vec<Coord> {
.collect()
}

fn build_flexpolyline(coords: &[Coord], precision: flexpolyline::Precision) -> flexpolyline::Polyline {
let coords = coords.iter().map(|c| (c.x, c.y));
flexpolyline::Polyline::Data2d {
coordinates: coords.collect(),
precision2d: precision,
}
}

#[allow(unused_must_use)]
fn bench_encode(c: &mut Criterion) {
let coords = build_coords();
Expand All @@ -35,12 +43,20 @@ fn bench_encode(c: &mut Criterion) {
black_box(encode_coordinates(coords.iter().copied(), 6).unwrap());
})
});

// This is just to compare us to another popular library. The format isn't identical so we
// don't expet performance to be identical, but it's some kind of touchstone.
c.bench_function("encode 10_000 coordinates at precision 1e-5 (flexpolyline)", |b| {
let pl = build_flexpolyline(&coords, flexpolyline::Precision::Digits5);
b.iter(|| {
black_box(pl.encode().unwrap());
})
});
}

#[allow(unused_must_use)]
fn bench_decode(c: &mut Criterion) {
let coords = build_coords();

c.bench_function("decode 10_000 coordinates at precision 1e-5", |b| {
let encoded = encode_coordinates(coords.iter().copied(), 5).unwrap();
b.iter(|| {
Expand All @@ -54,6 +70,15 @@ fn bench_decode(c: &mut Criterion) {
black_box(decode_polyline(&encoded, 6).unwrap());
})
});

// This is just to compare us to another popular library. The format isn't identical so we
// don't expet performance to be identical, but it's some kind of touchstone.
c.bench_function("decode 10_000 coordinates at precision 1e-5 (flexpolyline)", |b| {
let encoded = build_flexpolyline(&coords, flexpolyline::Precision::Digits5).encode().unwrap();
b.iter(|| {
black_box(flexpolyline::Polyline::decode(&encoded).unwrap());
})
});
}

criterion_group!(benches, bench_encode, bench_decode,);
Expand Down

0 comments on commit a3a4028

Please sign in to comment.