Skip to content

Commit

Permalink
perf(Math): Avoid Math.min, Math.max in vtkMath.arrayMin, vtkMath.arr…
Browse files Browse the repository at this point in the history
…ayMax

Timings:

  Existing Math.min: ~210ms for arrayMin, ~2712ms for entire volume rendering initialization
  Comparison operator: ~35ms for arrayMin, ~2070ms for entire volume rendering initialization
  Ternary operator: ~37ms for arrayMin, ~2125ms for entire volume rendering initialization
  • Loading branch information
thewtex committed Feb 12, 2018
1 parent 5c77a5a commit 9ef52d7
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions Sources/Common/Core/Math/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const { vtkErrorMacro, vtkWarningMacro } = macro;
let randomSeedValue = 0;
const VTK_MAX_ROTATIONS = 20;
const VTK_SMALL_NUMBER = 1.0e-12;
const MAX_FUNCTION_ARGUMENTS = 32768;

function notImplemented(method) {
return () => vtkErrorMacro(`vtkMath::${method} - NOT IMPLEMENTED`);
Expand Down Expand Up @@ -45,25 +44,21 @@ const { round, floor, ceil, min, max } = Math;

function arrayMin(arr) {
let minValue = Infinity;
for (let i = 0, len = arr.length; i < len; i += MAX_FUNCTION_ARGUMENTS) {
const submin = Math.min.apply(
null,
arr.slice(i, Math.min(i + MAX_FUNCTION_ARGUMENTS, len))
);
minValue = Math.min(submin, minValue);
for (let i = 0, len = arr.length; i < len; ++i) {
if (arr[i] < minValue) {
minValue = arr[i];
}
}

return minValue;
}

function arrayMax(arr) {
let maxValue = -Infinity;
for (let i = 0, len = arr.length; i < len; i += MAX_FUNCTION_ARGUMENTS) {
const submax = Math.max.apply(
null,
arr.slice(i, Math.min(i + MAX_FUNCTION_ARGUMENTS, len))
);
maxValue = Math.max(submax, maxValue);
for (let i = 0, len = arr.length; i < len; ++i) {
if (arr[i] > maxValue) {
maxValue = arr[i];
}
}

return maxValue;
Expand Down

0 comments on commit 9ef52d7

Please sign in to comment.