Expensive use of a standard algorithm when a more efficient method exists
Functions from the algorithm library are misused with
inappropriate inputs, resulting in inefficient code
Since R2021b
Description
Polyspace® reports a defect when you misuse functions in the algorithm
library with a container, which results in inefficient code. You might be recalculating known
or constant information about the container or using a function that is inappropriate for the
container. The scenarios that trigger this defect include:
Running a search by calling functions such as
std::find,std::equal_range,std::upper_bound,std::binary_search, andstd::lower_boundwith associative containers such asstd::set,std::map,std::multiset, andstd::mutimap.Checking for the presence of a certain key in a container by calling
std::count. That is, you convert the output ofstd::countto aboolor compare the output to either0or1.Checking for a sorted associative container by calling
std::is_sorted.Calculating the size of an associative container by calling
std::distance.Searching for consecutive equal elements in unique-value containers such as
std::setorstd::mapby callingstd::adjacent_find.Passing expensive functors by value to standard functions.
Risk
Misusing the functions from the algorithm library might result in
inefficient code or unexpected behavior.
Searching associative containers by using linear search functions, such as
std::findandstd::binary_search, is inefficient.When checking for a certain key in a container, performing an exhaustive search for every instance of the key by calling
std::countis unnecessary and inefficient.If an associative container is already sorted, calling
std::is_sortedis unnecessary. If the associative container is not sorted, the output ofstd::is_sortedmight be indeterminate, resulting in unexpected behavior.Calculating the size of an associative container by calling its
sizemethod is more efficient than using thestd::distancefunction.Calling
std::adjacent_findwith unique-value containers such asstd::setorstd::mapalways results inend(). Because the output is constant, the call is unnecessary.If you pass a functor to a standard algorithm function, it is always copied. If the functor is expensive, this copy operation makes your code expensive and inefficient.
Fix
To fix this defect, refactor your code to use the standard algorithm
functions more efficiently.
When searching in associative containers, avoid using
stdfunctions. If possible, use the search method of the container, such asstd::set::find.When checking for the presence of a certain key, call the
findorcontains(C++20) method of the container. For containers that do not implement these methods, usestd::findinstead ofstd::count.Avoid using
std::is_sortedorstd::adjacent_findwith associative containers.To check the size of an associative container, use the
sizemethod of the container. Avoid usingstd::distanceto calculate size.Avoid passing expensive-to-copy functors to
stdfunctions. Instead, create a wrapper functor that contains a reference to the original functor and pass the wrapper to standard algorithm functions.
Performance improvements might vary based on the compiler, library implementation, and environment that you are using.
Examples
Result Information
| Group: Performance |
| Language: C++ |
| Default: Off |
Command-Line Syntax:
EXPENSIVE_USE_OF_STD_ALGORITHM |
| Impact: Medium |
Version History
Introduced in R2021bSee Also
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)