Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does curly brace scope for comment purpose slow down C++ code?

Tags:

c++

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";
}
like image 366
waterd Avatar asked Nov 20 '17 16:11

waterd


People also ask

What do curly braces do in C?

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.

Are curly brackets necessary in C?

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.

What do curly braces denote in C Why does it make sense to use curly braces to surround the body of a function?

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.

Why do we use block of statements with braces in C?

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.

Why do we use curly braces in C++?

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.

Should curly braces be used in BCPL and ALGOL?

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.

What are curly brackets used for in programming?

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 }.

What is a closing brace in C?

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.


2 Answers

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.

like image 139
pat Avatar answered Nov 15 '22 00:11

pat


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.

like image 43
cmaster - reinstate monica Avatar answered Nov 14 '22 22:11

cmaster - reinstate monica