Created
July 22, 2016 07:01
-
-
Save zarch/3cda1ffda67e156d712f5c1b5a3d3d23 to your computer and use it in GitHub Desktop.
zip vs windows
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)] | |
extern crate test; | |
extern crate num; | |
extern crate geo; | |
use test::Bencher; | |
use num::Float; | |
use geo::{Coordinate, Point, LineString}; | |
pub static NITERATIONS: i32 = 10000; | |
fn intersects_zip<T>(aline: &LineString<T>, bline: &LineString<T>) -> bool | |
where T: Float | |
{ | |
let vect0 = &aline.0; | |
let vect1 = &bline.0; | |
if vect0.is_empty() || vect1.is_empty() { | |
return false; | |
} | |
for (a1, a2) in vect0.iter().zip(vect0[1..].iter()) { | |
for (b1, b2) in vect1.iter().zip(vect1[1..].iter()) { | |
let u_b = (b2.y() - b1.y()) * (a2.x() - a1.x()) - | |
(b2.x() - b1.x()) * (a2.y() - a1.y()); | |
if u_b == T::zero() { | |
continue; | |
} | |
let ua_t = (b2.x() - b1.x()) * (a1.y() - b1.y()) - | |
(b2.y() - b1.y()) * (a1.x() - b1.x()); | |
let ub_t = (a2.x() - a1.x()) * (a1.y() - b1.y()) - | |
(a2.y() - a1.y()) * (a1.x() - b1.x()); | |
let u_a = ua_t / u_b; | |
let u_b = ub_t / u_b; | |
if (T::zero() <= u_a) && (u_a <= T::one()) && (T::zero() <= u_b) && (u_b <= T::one()) { | |
return true; | |
} | |
} | |
} | |
false | |
} | |
fn intersects_win<T>(aline: &LineString<T>, bline: &LineString<T>) -> bool | |
where T: Float | |
{ | |
let vect0 = &aline.0; | |
let vect1 = &bline.0; | |
if vect0.is_empty() || vect1.is_empty() { | |
return false; | |
} | |
for a in vect0.windows(2).into_iter() { | |
for b in vect1.windows(2).into_iter() { | |
let u_b = (b[1].y() - b[0].y()) * (a[1].x() - a[0].x()) - | |
(b[1].x() - b[0].x()) * (a[1].y() - a[0].y()); | |
if u_b == T::zero() { | |
continue; | |
} | |
let ua_t = (b[1].x() - b[0].x()) * (a[0].y() - b[0].y()) - | |
(b[1].y() - b[0].y()) * (a[0].x() - b[0].x()); | |
let ub_t = (a[1].x() - a[0].x()) * (a[0].y() - b[0].y()) - | |
(a[1].y() - a[0].y()) * (a[0].x() - b[0].x()); | |
let u_a = ua_t / u_b; | |
let u_b = ub_t / u_b; | |
if (T::zero() <= u_a) && (u_a <= T::one()) && (T::zero() <= u_b) && (u_b <= T::one()) { | |
return true; | |
} | |
} | |
} | |
false | |
} | |
#[bench] | |
fn bench_zip(b: &mut Bencher) { | |
let p = |x, y| Point(Coordinate { x: x, y: y }); | |
let vect_points0 = |len| { | |
let mut vect = Vec::<Point<f64>>::new(); | |
for coord in 0..len { | |
vect.push(p(coord as f64, coord as f64)); | |
} | |
vect | |
}; | |
let vect_points1 = |len| { | |
let mut vect = Vec::<Point<f64>>::new(); | |
for coord in 0..len { | |
vect.push(p(coord as f64, len as f64 - coord as f64)); | |
} | |
vect | |
}; | |
let aline = LineString(vect_points0(NITERATIONS)); | |
let bline = LineString(vect_points1(NITERATIONS)); | |
b.iter(|| intersects_zip(&aline, &bline)); | |
} | |
#[bench] | |
fn bench_win(b: &mut Bencher) { | |
let p = |x, y| Point(Coordinate { x: x, y: y }); | |
let vect_points0 = |len| { | |
let mut vect = Vec::<Point<f64>>::new(); | |
for coord in 0..len { | |
vect.push(p(coord as f64, coord as f64)); | |
} | |
vect | |
}; | |
let vect_points1 = |len| { | |
let mut vect = Vec::<Point<f64>>::new(); | |
for coord in 0..len { | |
vect.push(p(coord as f64, len as f64 - coord as f64)); | |
} | |
vect | |
}; | |
let aline = LineString(vect_points0(NITERATIONS)); | |
let bline = LineString(vect_points1(NITERATIONS)); | |
b.iter(|| intersects_win(&aline, &bline)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment