Skip to content

Commit

Permalink
Scalar field improvements (#103)
Browse files Browse the repository at this point in the history
* ScalarField: make m_minValue and m_maxValue private

Opens opportunity to e.g. compute lazily.

* ScalarField: optimize computeMinAndMax

Use local variable for performance (compiler can't optimize store due to
exception possibility). Reduces execution time approximately 30%.

Also properly clears min and max if the field is not empty, but all
points are invalid.
  • Loading branch information
tpwrules authored May 30, 2024
1 parent 40754bd commit 2f1cfcf
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions include/ScalarField.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ namespace CCCoreLib
//! Scalar field name
char m_name[256];

private:
//! Minimum value
ScalarType m_minVal;
//! Maximum value
Expand All @@ -102,31 +103,36 @@ namespace CCCoreLib

inline void ScalarField::computeMinAndMax()
{
if (!empty())
ScalarType minVal, maxVal;

bool minMaxInitialized = false;
for (std::size_t i = 0; i < size(); ++i)
{
bool minMaxInitialized = false;
for (std::size_t i = 0; i < size(); ++i)
const ScalarType& val = at(i);
if (ValidValue(val))
{
const ScalarType& val = at(i);
if (ValidValue(val))
if (minMaxInitialized)
{
if (val < minVal)
minVal = val;
else if (val > maxVal)
maxVal = val;
}
else
{
if (minMaxInitialized)
{
if (val < m_minVal)
m_minVal = val;
else if (val > m_maxVal)
m_maxVal = val;
}
else
{
//first valid value is used to init min and max
m_minVal = m_maxVal = val;
minMaxInitialized = true;
}
//first valid value is used to init min and max
minVal = maxVal = val;
minMaxInitialized = true;
}
}
}
else //particular case: no value

if (minMaxInitialized)
{
m_minVal = minVal;
m_maxVal = maxVal;
}
else //particular case: zero valid values
{
m_minVal = m_maxVal = 0;
}
Expand Down

0 comments on commit 2f1cfcf

Please sign in to comment.