Created
July 19, 2016 04:54
-
-
Save zarch/3b03d3c5ac44f904f96aa699da383df9 to your computer and use it in GitHub Desktop.
Benchmark get_bbox function
This file contains hidden or 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
| #![feature(test)] | |
| // result on my machine: | |
| // test bench_get_bbox ... bench: 1,454,908 ns/iter (+/- 140,500) | |
| // test bench_get_bbox_fold ... bench: 2,744,768 ns/iter (+/- 302,968) | |
| extern crate test; | |
| extern crate num; | |
| extern crate geo; | |
| use test::Bencher; | |
| use num::Float; | |
| use geo::{Bbox, Coordinate, Point}; | |
| pub static NITERATIONS: i32 = 1000000; | |
| fn get_min_max<T>(p: T, min: T, max: T) -> (T, T) | |
| where T: Float | |
| { | |
| if p > max {(min, p)} else if p < min {(p, max)} else {(min, max)} | |
| } | |
| fn get_bbox<T>(vect: &Vec<Point<T>>) -> Option<Bbox<T>> | |
| where T: Float | |
| { | |
| if vect.is_empty() { | |
| return None; | |
| } | |
| let mut xrange = (vect[0].x(), vect[0].x()); | |
| let mut yrange = (vect[0].y(), vect[0].y()); | |
| for pnt in vect[1..].iter() { | |
| let (px, py) = (pnt.x(), pnt.y()); | |
| xrange = get_min_max(px, xrange.0, xrange.1); | |
| yrange = get_min_max(py, yrange.0, yrange.1); | |
| } | |
| Some(Bbox{xmin: xrange.0, xmax: xrange.1, | |
| ymin: yrange.0, ymax: yrange.1}) | |
| } | |
| fn update_bbox_from_point<T>(bbox: &Bbox<T>, point: &Point<T>) -> Bbox<T> | |
| where T: Float | |
| { | |
| let (px, py) = (point.x(), point.y()); | |
| let xrange = get_min_max(px, bbox.xmin, bbox.xmax); | |
| let yrange = get_min_max(py, bbox.ymin, bbox.ymax); | |
| Bbox{xmin: xrange.0, xmax: xrange.1, ymin: yrange.0, ymax: yrange.1} | |
| } | |
| pub fn get_bbox_fold<T>(vect: &Vec<Point<T>>) -> Option<Bbox<T>> | |
| where T: Float | |
| { | |
| if vect.is_empty() { | |
| return None; | |
| } else { | |
| let bbox = Bbox{xmin: vect[0].x(), xmax: vect[0].x(), | |
| ymin: vect[0].y(), ymax: vect[0].y()}; | |
| Some(vect[1..].iter().fold(bbox, |bbox, point| update_bbox_from_point(&bbox, point))) | |
| } | |
| } | |
| #[bench] | |
| fn bench_get_bbox(b: &mut Bencher) { | |
| let p = |x, y| Point(Coordinate { x: x, y: y }); | |
| let vect_points = |len| { | |
| let mut vect = Vec::<Point<f64>>::new(); | |
| for coord in 0..len { | |
| vect.push(p(coord as f64, coord as f64)); | |
| } | |
| vect | |
| }; | |
| let vpoints = vect_points(NITERATIONS); | |
| b.iter(|| get_bbox(&vpoints)); | |
| } | |
| #[bench] | |
| fn bench_get_bbox_fold(b: &mut Bencher) { | |
| let p = |x, y| Point(Coordinate { x: x, y: y }); | |
| let vect_points = |len| { | |
| let mut vect = Vec::<Point<f64>>::new(); | |
| for coord in 0..len { | |
| vect.push(p(coord as f64, coord as f64)); | |
| } | |
| vect | |
| }; | |
| let vpoints = vect_points(NITERATIONS); | |
| b.iter(|| get_bbox_fold(&vpoints)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment