From 6e581223802679b7705e8a2d437ccb32f7f248ae Mon Sep 17 00:00:00 2001 From: Joel Therrien Date: Mon, 10 Sep 2018 17:16:43 -0700 Subject: [PATCH] Optimize MathFunction --- .../randomforest/utils/MathFunction.java | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/main/java/ca/joeltherrien/randomforest/utils/MathFunction.java b/src/main/java/ca/joeltherrien/randomforest/utils/MathFunction.java index 68d6e83..3425787 100644 --- a/src/main/java/ca/joeltherrien/randomforest/utils/MathFunction.java +++ b/src/main/java/ca/joeltherrien/randomforest/utils/MathFunction.java @@ -35,20 +35,29 @@ public class MathFunction implements Serializable { } public Point evaluate(double time){ - final Optional pointOptional = points.stream() - .filter(point -> point.getTime() <= time) - .max(Comparator.comparingDouble(Point::getTime)); + Point point = defaultValue; - return pointOptional.orElse(defaultValue); + for(final Point currentPoint: points){ + if(currentPoint.getTime() > time){ + break; + } + point = currentPoint; + } + return point; } public Point evaluatePrevious(double time){ - final Optional pointOptional = points.stream() - .filter(point -> point.getTime() < time) - .max(Comparator.comparingDouble(Point::getTime)); + Point point = defaultValue; - return pointOptional.orElse(defaultValue); + for(final Point currentPoint: points){ + if(currentPoint.getTime() >= time){ + break; + } + point = currentPoint; + } + + return point; } @Override