Does the curly brace scope for clarifying code boundary increases code execution time? In my opinion, it does. Because exiting curly brace scope in C++ meaning stack unwinding and curly brace scope for comment purpose increases stack unwinding times. But I have no idea whether is expensive or not? Can I ignore the side effect?
You should focus on the code struture other than the code itself of the following code snippet.
#include <iostream>
#include <utility>
#include <vector>
#include <string>
int main()
{
std::string str = "Hello";
std::vector<std::string> v;
{// uses the push_back(const T&) overload, which means
// we'll incur the cost of copying str
v.push_back(str);
std::cout << "After copy, str is \"" << str << "\"\n";
//other code involves local variable
}
{// uses the rvalue reference push_back(T&&) overload,
// which means no strings will be copied; instead, the contents
// of str will be moved into the vector. This is less
// expensive, but also means str might now be empty.
v.push_back(std::move(str));
std::cout << "After move, str is \"" << str << "\"\n";
//other code involves local variable
}
std::cout << "The contents of the vector are \"" << v[0]
<< "\", \"" << v[1] << "\"\n";
}
In programming, curly braces (the { and } characters) are used in a variety of ways. In C/C++, they are used to signify the start and end of a series of statements. In the following expression, everything between the { and } are executed if the variable mouseDOWNinText is true. See event loop.
Master C and Embedded C Programming- Learn as you go So we can omit curly braces only there is a single statement under if-else or loop. Here in both of the cases, the Line1 is in the if block but Line2 is not in the if block. So if the condition fails, or it satisfies the Line2 will be executed always.
The curly braces is actually talking about scope. For..loop.. can be used without curly braces if the statement you want to loop consists of only one line of code.
Braces are used to delimit blocks in C. The brackets don't belong to check_eeprom_data() they only defines a scope in the code.
One of the reasons behind using curly braces in a code is for the semantics. But the more important purpose of these braces is that they define the scope of a particular line of code of the variables being used in it.
As programmers were already familiar with square brackets for arrays in Algol and BCPL, and curly braces for blocks in BCPL, there was little need or desire to change this when making another language. The updated question includes an addendum of productivity for curly brace usage and mentions python.
BCPL was the first curly bracket programming language, and the curly brackets survived the syntactical changes and have become a common means of denoting program source code statements. In practice, on limited keyboards of the day, source programs often used the sequences $ ( and $) in place of the symbols { and }.
A closing brace closes a scope, and this calls the destructor of all the objects that were declared inside that scope. And calling the code of those destructors can do dozens of things, from freeing memory to closing a database handle to shutting down a file: This is the fundamental C++ idiom of RAII.
Exiting a scope destroys all local variables declared in that scope (youngest first). In your example there are no local variables declared in the scopes, so there should be no overhead. You can confirm this in this case by compiling to assembly language (-s
under gcc), with and without the braces and comparing the result.
On the contrary: The extra scopes may speed things up. (However, the effect is so slight that you should not bother!)
You see, within a function, the compiler has full control over all the variables the function uses. It will not push some extra variables on the stack where you declare them, and pop them off when they go out of scope. Instead, it will create a stack frame with sufficient space to hold all the local variables that you need. Your compiler is generally smart enough to reuse the space within this stack frame: When one int
goes out of scope before another int
gets declared, that second int
may reuse the slot of the first in the stack frame.
So, when you reduce the scopes of your local variables, you allow the compiler to reuse more slots in the stack frame. This reduces the overall size of your stack, and the distances by which the stack grows/shrinks. This in turn leads to better cache usage, and thus to better performance.
Nevertheless, the impact of this effect is slight, so you should generally ignore it and just write the most readable code you can.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With