Skip to content

Commit

Permalink
Small performance optimization in Vector::Equals.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rpavlik authored and whitequark committed May 13, 2019
1 parent 838126f commit 52a481c
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 52a481c

Please sign in to comment.