From 52a481cd1eee85454f9dafb6331452841231ebbd Mon Sep 17 00:00:00 2001 From: "Ryan A. Pavlik" Date: Mon, 13 May 2019 10:02:50 -0500 Subject: [PATCH] Small performance optimization in Vector::Equals. This function showed up surprisingly high on a CPU time profile when the GUI was unresponsive "doing things". Removed a duplicated difference in the not-equal case, and switched to abs and a single compare instead of two compares with a negation. It seems to have moved the function further down in the profile. --- src/util.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/util.cpp b/src/util.cpp index b0dde547..355922ac 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -441,11 +441,12 @@ double Vector::Element(int i) const { bool Vector::Equals(Vector v, double tol) const { // Quick axis-aligned tests before going further - double dx = v.x - x; if(dx < -tol || dx > tol) return false; - double dy = v.y - y; if(dy < -tol || dy > tol) return false; - double dz = v.z - z; if(dz < -tol || dz > tol) return false; + const Vector dv = this->Minus(v); + if (fabs(dv.x) > tol) return false; + if (fabs(dv.y) > tol) return false; + if (fabs(dv.z) > tol) return false; - return (this->Minus(v)).MagSquared() < tol*tol; + return dv.MagSquared() < tol*tol; } bool Vector::EqualsExactly(Vector v) const {