Skip to content

Commit

Permalink
PERF: Prefer compiletime computation of scale factor
Browse files Browse the repository at this point in the history
The ivar m_Factor was a constant that did not require
state to be saved.  The computation can be done at
compile time, thus saving runtime computations.

Move m_Factor from an ivar to a constexpr in the one place
it is used.
  • Loading branch information
hjmjohnson committed Dec 17, 2024
1 parent d786f75 commit f65892a
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions Modules/Core/Common/include/itkGaussianKernelFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,18 @@ class ITK_TEMPLATE_EXPORT GaussianKernelFunction : public KernelFunctionBase<TRe
TRealValueType
Evaluate(const TRealValueType & u) const override
{
return (std::exp(TRealValueType{ -0.5 } * itk::Math::sqr(u)) * m_Factor);
constexpr auto negHalf = TRealValueType{ -0.5 };
return std::exp(negHalf * itk::Math::sqr(u)) * Math::one_over_sqrt2pi;
}

protected:
GaussianKernelFunction()
: m_Factor(TRealValueType{ 1.0 } / std::sqrt(TRealValueType{ 2.0 * itk::Math::pi }))
{}
GaussianKernelFunction() {}
~GaussianKernelFunction() override = default;
void
PrintSelf(std::ostream & os, Indent indent) const override
{
Superclass::PrintSelf(os, indent);
}

private:
const TRealValueType m_Factor{};
};
} // end namespace itk

Expand Down

0 comments on commit f65892a

Please sign in to comment.