|
| 1 | +use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main}; |
| 2 | +use geo::algorithm::{Contains, Covers}; |
| 3 | +use geo::{Convert, wkt}; |
| 4 | +use geo::{coord, geometry::*}; |
| 5 | + |
| 6 | +fn rect_covers(c: &mut Criterion) { |
| 7 | + c.bench_function("rect covers line", |bencher| { |
| 8 | + let rect = Rect::new(coord! { x: 0., y: 0. }, coord! { x: 10., y: 10. }); |
| 9 | + let line = Line::new(coord! { x: 5., y: 5. }, coord! { x: 7., y: 7. }); |
| 10 | + bencher.iter(|| { |
| 11 | + assert!(criterion::black_box(&rect).covers(criterion::black_box(&line))); |
| 12 | + }); |
| 13 | + }); |
| 14 | + |
| 15 | + c.bench_function("rect contains line", |bencher| { |
| 16 | + let rect = Rect::new(coord! { x: 0., y: 0. }, coord! { x: 10., y: 10. }); |
| 17 | + let line = Line::new(coord! { x: 5., y: 5. }, coord! { x: 7., y: 7. }); |
| 18 | + bencher.iter(|| { |
| 19 | + assert!(criterion::black_box(&rect).contains(criterion::black_box(&line))); |
| 20 | + }); |
| 21 | + }); |
| 22 | +} |
| 23 | + |
| 24 | +fn linestring_covers_point(c: &mut Criterion) { |
| 25 | + c.bench_function("linestring covers point", |bencher| { |
| 26 | + let ls: LineString<f64> = wkt! {LINESTRING(0 0, 10 0, 10 10, 0 10)}.convert(); |
| 27 | + let pt: Point<f64> = wkt! {POINT(5 10)}.convert(); |
| 28 | + bencher.iter(|| { |
| 29 | + assert!(criterion::black_box(&ls).covers(criterion::black_box(&pt))); |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + c.bench_function("linestring contains point", |bencher| { |
| 34 | + let ls: LineString<f64> = wkt! {LINESTRING(0 0, 10 0, 10 10, 0 10)}.convert(); |
| 35 | + let pt: Point<f64> = wkt! {POINT(5 10)}.convert(); |
| 36 | + bencher.iter(|| { |
| 37 | + assert!(criterion::black_box(&ls).contains(criterion::black_box(&pt))); |
| 38 | + }); |
| 39 | + }); |
| 40 | +} |
| 41 | + |
| 42 | +// bench a method derived from intersects |
| 43 | +fn rect_linestring_scaling(c: &mut Criterion) { |
| 44 | + fn make_outer_rect(n: i32) -> Rect<f64> { |
| 45 | + Rect::new(coord! {x:0,y:0}, coord! {x:n,y:3}).convert() |
| 46 | + } |
| 47 | + fn make_inner_ls(n: i32) -> LineString<f64> { |
| 48 | + LineString::new((1..n).map(|i: i32| coord! {x:i,y:(1+i%2)}).collect()).convert() |
| 49 | + } |
| 50 | + |
| 51 | + { |
| 52 | + // create two polygons, both of of n+2 sides and no holes |
| 53 | + let mut group = c.benchmark_group("covers rect linestring scaling"); |
| 54 | + |
| 55 | + // trait is faster for small polygons, but relate overtakes from around 700*700 boundary segment checks |
| 56 | + for i in [10, 1_000, 100_000] { |
| 57 | + group.throughput(Throughput::Elements(i as u64)); |
| 58 | + |
| 59 | + let inner_poly = make_inner_ls(i); |
| 60 | + let outer_poly = make_outer_rect(i); |
| 61 | + |
| 62 | + group.bench_with_input( |
| 63 | + BenchmarkId::new("covers trait", i), |
| 64 | + &(&outer_poly, &inner_poly), |
| 65 | + |bencher, &(a, b)| { |
| 66 | + bencher.iter(|| assert!(a.covers(b))); |
| 67 | + }, |
| 68 | + ); |
| 69 | + |
| 70 | + // contains delegates this to relate |
| 71 | + group.bench_with_input( |
| 72 | + BenchmarkId::new("contains trait", i), |
| 73 | + &(&outer_poly, &inner_poly), |
| 74 | + |bencher, &(a, b)| { |
| 75 | + bencher.iter(|| assert!(a.contains(b))); |
| 76 | + }, |
| 77 | + ); |
| 78 | + } |
| 79 | + group.finish(); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +criterion_group!( |
| 84 | + benches, |
| 85 | + rect_covers, |
| 86 | + linestring_covers_point, |
| 87 | + rect_linestring_scaling |
| 88 | +); |
| 89 | +criterion_main!(benches); |
0 commit comments