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_bound
with 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::count
to abool
or compare the output to either0
or1
.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::set
orstd::map
by 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::find
andstd::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::count
is unnecessary and inefficient.If an associative container is already sorted, calling
std::is_sorted
is unnecessary. If the associative container is not sorted, the output ofstd::is_sorted
might be indeterminate, resulting in unexpected behavior.Calculating the size of an associative container by calling its
size
method is more efficient than using thestd::distance
function.Calling
std::adjacent_find
with unique-value containers such asstd::set
orstd::map
always 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
std
functions. If possible, use the search method of the container, such asstd::set::find
.When checking for the presence of a certain key, call the
find
orcontains
(C++20) method of the container. For containers that do not implement these methods, usestd::find
instead ofstd::count
.Avoid using
std::is_sorted
orstd::adjacent_find
with associative containers.To check the size of an associative container, use the
size
method of the container. Avoid usingstd::distance
to calculate size.Avoid passing expensive-to-copy functors to
std
functions. 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)