Skip to content

Commit

Permalink
Merge branch 'master' into nalgebra
Browse files Browse the repository at this point in the history
  • Loading branch information
gammelalf committed Jun 8, 2022
2 parents b570188 + 33ae317 commit c7bc8be
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 45 deletions.
51 changes: 34 additions & 17 deletions benches/common/bezier.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,49 @@
use criterion::{black_box, Criterion};
use gammalg::bezier::BezierCurve;
use crate::common::samples::CURVES;

macro_rules! bench_curve_types {
($group:ident, $body:expr) => {
$group.bench_function("linear", |b| {
for curve in CURVES.LINEAR.iter() {
b.iter(|| $body(curve))
}
});
$group.bench_function("quadratic", |b| {
for curve in CURVES.QUADRATIC.iter() {
b.iter(|| $body(curve))
}
});
$group.bench_function("cubic", |b| {
for curve in CURVES.CUBIC.iter() {
b.iter(|| $body(curve))
}
});
$group.bench_function("higher", |b| {
for curve in CURVES.HIGHER.iter() {
b.iter(|| $body(curve))
}
});
}
}

pub fn split(c: &mut Criterion) {
c.bench_function("split", |b| {
for curve in CURVES.iter() {
b.iter(|| black_box(curve.split(0.5)))
}
});
let mut g = c.benchmark_group("BezierCurve::split");
bench_curve_types!(g, |curve: &BezierCurve<f64>| curve.split(black_box(0.5)));
}

pub fn eval(c: &mut Criterion) {
c.bench_function("eval", |b| {
for curve in CURVES.iter() {
b.iter(|| black_box(curve.castlejau_eval(0.5)))
}
});
pub fn castlejau_eval(c: &mut Criterion) {
let mut g = c.benchmark_group("BezierCurve::castlejau_eval");
bench_curve_types!(g, |curve: &BezierCurve<f64>| curve.castlejau_eval(black_box(0.5)));
}

pub fn normal(c: &mut Criterion) {
c.bench_function("normal", |b| {
for curve in CURVES.iter() {
b.iter(|| black_box(curve.normal(0.5)))
}
});
let mut g = c.benchmark_group("BezierCurve::normal");
bench_curve_types!(g, |curve: &BezierCurve<f64>| curve.normal(black_box(0.5)));
}

pub fn all(c: &mut Criterion) {
split(c);
eval(c);
castlejau_eval(c);
normal(c);
}
114 changes: 88 additions & 26 deletions benches/common/samples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,91 @@ use smallvec::smallvec;
use nalgebra::Vector2;
use gammalg::bezier::BezierCurve;

pub static CURVES: Lazy<Vec<BezierCurve<f64>>> = Lazy::new(|| vec![
BezierCurve(smallvec![
Vector2::new(0.0, 0.0),
Vector2::new(10.0, 0.0),
Vector2::new(0.0, 10.0),
Vector2::new(10.0, 10.0),
]),
BezierCurve(smallvec![
Vector2::new(50.0, 0.0),
Vector2::new(200.0, 33.0),
Vector2::new(0.0, 66.0),
Vector2::new(50.0, 100.0),
]),
BezierCurve(smallvec![
Vector2::new(50.0, 0.0),
Vector2::new(155.0, 23.1),
Vector2::new(88.5, 46.2),
Vector2::new(56.3, 69.643)
]),
BezierCurve(smallvec![
Vector2::new(56.3, 69.643),
Vector2::new(42.5, 79.69),
Vector2::new(35.0, 89.8),
Vector2::new(50.0, 100.0)
]),
]);
/// Points generated randomly
/// ```python
/// from random import random
/// for i in range(10):
/// print(f"Vector2::new({(random()-0.5)*i}, {(random()-0.5)*i})")
/// ```
pub static POINTS: [Vector2<f64>; 10] = [
Vector2::new( 0.0, 0.0 ),
Vector2::new(-0.29734, 0.44984),
Vector2::new(-0.52560, 0.42885),
Vector2::new( 1.42777, -0.02652),
Vector2::new( 1.98032, -0.67824),
Vector2::new( 0.44863, -0.91328),
Vector2::new(-2.51139, -0.79100),
Vector2::new(-3.10479, -0.59318),
Vector2::new(-1.16022, -2.95591),
Vector2::new(-1.07946, 0.78888),
];

pub static CURVES: Lazy<Curves> = Lazy::new(Curves::new);
#[allow(non_snake_case)]
pub struct Curves {
pub LINEAR: Vec<BezierCurve<f64>>,
pub QUADRATIC: Vec<BezierCurve<f64>>,
pub CUBIC: Vec<BezierCurve<f64>>,
pub HIGHER: Vec<BezierCurve<f64>>,
}
impl Curves {
pub fn new() -> Curves {
Curves {
LINEAR: vec![
BezierCurve([0, 1].into_iter().map(|i| POINTS[i]).collect()),
BezierCurve([1, 2].into_iter().map(|i| POINTS[i]).collect()),
BezierCurve([2, 3].into_iter().map(|i| POINTS[i]).collect()),
BezierCurve([3, 4].into_iter().map(|i| POINTS[i]).collect()),
BezierCurve([4, 5].into_iter().map(|i| POINTS[i]).collect()),
BezierCurve([5, 6].into_iter().map(|i| POINTS[i]).collect()),
BezierCurve([6, 7].into_iter().map(|i| POINTS[i]).collect()),
BezierCurve([7, 8].into_iter().map(|i| POINTS[i]).collect()),
BezierCurve([8, 9].into_iter().map(|i| POINTS[i]).collect()),
BezierCurve([9, 0].into_iter().map(|i| POINTS[i]).collect()),
],
QUADRATIC: vec![
BezierCurve([0, 1, 2].into_iter().map(|i| POINTS[i]).collect()),
BezierCurve([2, 3, 4].into_iter().map(|i| POINTS[i]).collect()),
BezierCurve([4, 6, 8].into_iter().map(|i| POINTS[i]).collect()),
BezierCurve([1, 3, 5].into_iter().map(|i| POINTS[i]).collect()),
BezierCurve([5, 7, 9].into_iter().map(|i| POINTS[i]).collect()),
BezierCurve([0, 2, 1].into_iter().map(|i| POINTS[i]).collect()),
BezierCurve([2, 4, 3].into_iter().map(|i| POINTS[i]).collect()),
BezierCurve([4, 8, 6].into_iter().map(|i| POINTS[i]).collect()),
BezierCurve([1, 5, 3].into_iter().map(|i| POINTS[i]).collect()),
BezierCurve([5, 9, 7].into_iter().map(|i| POINTS[i]).collect()),
],
CUBIC: vec![
BezierCurve([0, 1, 2, 3].into_iter().map(|i| POINTS[i]).collect()),
BezierCurve([2, 3, 4, 5].into_iter().map(|i| POINTS[i]).collect()),
BezierCurve([4, 5, 6, 7].into_iter().map(|i| POINTS[i]).collect()),
BezierCurve([6, 7, 8, 9].into_iter().map(|i| POINTS[i]).collect()),
BezierCurve([8, 9, 0, 1].into_iter().map(|i| POINTS[i]).collect()),
BezierCurve([0, 4, 2, 6].into_iter().map(|i| POINTS[i]).collect()),
BezierCurve([1, 5, 3, 7].into_iter().map(|i| POINTS[i]).collect()),
BezierCurve([2, 6, 4, 8].into_iter().map(|i| POINTS[i]).collect()),
BezierCurve([3, 7, 5, 9].into_iter().map(|i| POINTS[i]).collect()),
BezierCurve([4, 8, 6, 0].into_iter().map(|i| POINTS[i]).collect()),
],
HIGHER: vec![
BezierCurve([0, 1, 2, 3, 4].into_iter().map(|i| POINTS[i]).collect()),
BezierCurve([2, 3, 4, 5, 6].into_iter().map(|i| POINTS[i]).collect()),
BezierCurve([4, 5, 6, 7, 8].into_iter().map(|i| POINTS[i]).collect()),
BezierCurve([6, 7, 8, 9, 0].into_iter().map(|i| POINTS[i]).collect()),
BezierCurve([8, 9, 0, 1, 2].into_iter().map(|i| POINTS[i]).collect()),
BezierCurve([0, 4, 2, 6, 8].into_iter().map(|i| POINTS[i]).collect()),
BezierCurve([1, 5, 3, 7, 9].into_iter().map(|i| POINTS[i]).collect()),
BezierCurve([2, 6, 4, 8, 0].into_iter().map(|i| POINTS[i]).collect()),
BezierCurve([3, 7, 5, 9, 1].into_iter().map(|i| POINTS[i]).collect()),
BezierCurve([4, 8, 6, 0, 2].into_iter().map(|i| POINTS[i]).collect()),
],
}
}

pub fn iter(&self) -> impl Iterator<Item=&BezierCurve<f64>> {
self.LINEAR.iter()
.chain(self.QUADRATIC.iter())
.chain(self.CUBIC.iter())
.chain(self.HIGHER.iter())
}
}
6 changes: 5 additions & 1 deletion benches/flamegraph.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use std::time::Duration;
use criterion::{criterion_group, criterion_main, Criterion};
use pprof::criterion::{PProfProfiler, Output};
mod common;

static SECOND: Duration = Duration::from_secs(1);
criterion_group!{
name = benches;
config = Criterion::default().with_profiler(PProfProfiler::new(100, Output::Flamegraph(None)));
config = Criterion::default()
.with_profiler(PProfProfiler::new(100, Output::Flamegraph(None)))
;
targets = common::bezier::all
}
criterion_main!(benches);
7 changes: 6 additions & 1 deletion benches/speed.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
use std::time::Duration;
use criterion::{criterion_group, criterion_main, Criterion};
mod common;

static SECOND: Duration = Duration::from_secs(1);
criterion_group!{
name = benches;
config = Criterion::default();
config = Criterion::default()
.warm_up_time(SECOND)
.measurement_time(SECOND)
;
targets = common::bezier::all
}
criterion_main!(benches);

0 comments on commit c7bc8be

Please sign in to comment.