Performance Defects
These defects detect issues that can lead to performance bottlenecks in C++ code. The detected issues include:
Issues that inadvertently cause copy instead of move operations
Inefficient or unnecessary temporary variable creation
Use of a function that has a possibly more efficient alternative
Polyspace Results
Copy Instead of Move Operation
A move operation
may throw | Throwing move operations might result in STL containers using the corresponding copy operations (Since R2020b) |
Const parameter
values may cause unnecessary data copies | Const parameter values may prevent a move operation resulting in a more performance-intensive copy operation (Since R2020a) |
Const return
values may cause unnecessary data copies | Const return values may prevent a move operation resulting in a more performance-intensive copy operation (Since R2020a) |
Const rvalue
reference parameter may cause unnecessary data copies | The const -ness of an rvalue reference prevents intended move
operation (Since R2021a) |
Const std::move
input may cause a more expensive object copy | Const std::move input cannot be moved and results in more
expensive copy operation (Since R2020b) |
Empty destructors
may cause unnecessary data copies | User-declared empty destructors prevent autogeneration of move constructors and move assignment operators (Since R2020a) |
Expensive return
of a const object | The return statement of a function copies an objects instead of moving it because
the returned object is declared as a const (Since R2022a) |
Move operation
uses copy | Non-const rvalue reference parameter of a function or operator is
copied instead of moved (Since R2021b) |
std::move called
on an unmovable type | std::move used on a class type with no move constructor or move
assignment operator (Since R2020b) |
Inefficient or Unnecessary Variable Creation
Expensive copy in
a range-based for loop iteration | The loop variable of a range-based for loop is copied from the
range elements instead of being referenced resulting in inefficient code (Since R2020b) |
Expensive local
variable copy | Local variable is created by copy from a const reference and not
modified later (Since R2021a) |
Expensive member
initialization | You assign values to class members in the constructor body instead of constructing members using a member initializer list (Since R2023b) |
Expensive pass by
value | Parameter might be expensive to copy (Since R2020b) |
Expensive return
by value | Functions return large output by value instead of by reference or pointer (Since R2020b) |
Expensive use of
std::any_cast | An object is cast by-value using std::any_cast when casting
by-reference is more efficient (Since R2023b) |
Expensive
construction of std::string or std::regex from constant string | A constant std::string or std::regex object is
constructed from constant data, resulting in inefficient code (Since R2020b) |
Missing constexpr
specifier | constexpr specifier can be used on variable or function for
compile-time evaluation (Since R2020b) |
Unnecessary
construction before reassignment | Instead of directly constructing objects with value, you construct objects and then immediately assign values to objects (Since R2023a) |
Expensive unused
object | Expensive local object is constructed but not used (Since R2024a) |
Expensive
std::function type definition | Definition of std::function type uses pass-by-value semantics
for arguments that are expensive to copy (Since R2024a) |
Function with More Efficient Alternative
Expensive dynamic
cast | Expensive dynamic_cast is used instead of more efficient
static_cast or const_cast (Since R2021b) |
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) |
Expensive use of
non-member std::string operator+() instead of a simple append | The non-member std::string operator+() function is called when
the append (or += ) method would have been more efficient (Since R2020b) |
Expensive use of
std::string methods instead of more efficient overload | An std::string method is called with a string literal of known
length, instead of a single quoted character (Since R2021a) |
Expensive use of
std::string with empty string literal | Use of std::string with empty string literal can be replaced by
less expensive calls to std::basic_string member functions (Since R2021a) |
Expensive use of
string functions from the C standard library | String functions from the C standard library are used inefficiently (Since R2022a) |
Expensive use of
substr() to shorten a std::string | The method std::string::substr() is called to shorten an
std::string object (Since R2022a) |
Inefficient use
of sprintf | The function sprintf , snprintf , or
swprintf copies strings instead of the more efficient
strcpy , strncopy , or
wcsncpy (Since R2021b) |
Inefficient
string length computation | String length calculated by using string length functions on return from
std::basic_string::c_str() instead of using
std::basic_string::length() (Since R2020a) |
std::endl may
cause an unnecessary flush | std::endl is used instead of the more efficient
\n (Since R2020a) |
Use of new or
make_unique instead of more efficient make_shared | Using new or make_unique to initialize or
reset shared_ptr results in additional memory allocation (Since R2021a) |
Unnecessary use
of std::string::c_str() or equivalent string methods | Instead of a std::string object, a string operation uses the
C-string obtained from std::string functions including
std::string::c_str , std::string::data() ,
std::string::at() , or std::string::operator[] ,
resulting in inefficient code (Since R2020b) |
Inappropriate Container use
Expensive use of
container's count method | The function member count() of a container is used for checking
if a key is present, leading to inefficient code (Since R2021b) |
Expensive use of
container's insertion methods | One of the insertion methods of a container is used to insert a temporary object (Since R2022a) |
Expensive use of
container's size method | A container's size() method is used for checking emptiness
instead of its empty() method, which is more efficient (Since R2022b) |
Expensive use of
map's bracket operator to insert or assign a value | The bracket operator of std::map or
std::unordered_map is used for inserting or assigning a value in the
container instead of the insert_or_assign() method, which is more
efficient (Since R2022b) |
Missing call to
container's reserve method | A fixed number of items are added to a container without calling the reserve() method of the container beforehand, resulting in inefficient code (Since R2022b) |
Expensive use of
map instead of set | The key for a map is member of the object being inserted, resulting in redundant map key (Since R2024b) |
Others
Expensive logical
operation | A logical operation requires the evaluation of both operands because of their order, resulting in inefficient code (Since R2021a) |
Expensive return
caused by unnecessary std::move | An unnecessary call to std::move in the return statement hinders
return value optimization, resulting in inefficient code (Since R2022b) |
Expensive
allocation in loop | Fixed sized memory is allocated or deallocated in a loop (Since R2022a) |
Expensive
post-increment operation | Object is post-incremented when pre-incrementing is faster (Since R2021b) |
Inefficient use
of for loop | Range-based for loop can perform equivalent iteration more efficiently (Since R2022a) |
Unnecessary
Padding | Members of a struct are padded to fulfill alignment requirement
when rearranging the members to fulfill this requirement saves memory (Since R2021b) |
Unnecessary
implementation of a special member function | Implementing special member functions hinders code optimization when implicit versions are equivalent (Since R2023a) |
Unnecessary
reference to parameter | Parameter is passed to function as const reference when passing
by value is more efficient (Since R2024a) |
Topics
- Bug Finder Defect Groups
The Bug Finder defect checkers are classified into groups such as data flow, concurrency, numerical, and so on.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)