在c ++程序中添加不必要的花括号{}会减慢它吗? [英] Does adding unnecessary curly brackets { } in a c++ program slow it down at all?

查看:119
本文介绍了在c ++程序中添加不必要的花括号{}会减慢它吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个愚蠢的问题,但我是新的C ++和编程一般,我找不到这里的答案。我知道在C ++中,{}在一些情况下是可选的。例如,如果你有一个简单的if语句,只执行一个操作,你不需要用{}包围它。

This might be a silly question, but I'm new to C++ and programming in general and I couldn't find the answer on here. I know in C++, the { } are optional in some cases. For example, if you have a simple if statement where only one operation is performed, you don't need to surround it with { }.

我只是想知道如果额外的括号对程序的速度有任何影响(即使是最小的)。我问的原因是因为我总是在所有的陈述中包括大括号,即使不是必需的,只是因为我喜欢封锁我的代码。

I was just wondering if the extra brackets have any effect (even the smallest) on the speed of the program. The reason I ask is because I always include the curly brackets in all of my statements even if not required, just because I like to block out my code.

我的个人喜好是:

   if (foo)
   {
        bar;
   }

而不是简单

   if (foo)
       bar;

我喜欢阅读代码时的样子。但是,如果这实际上对代码的速度有影响,这可能不是一个好主意。有人知道如果额外的括号影响速度?谢谢。

I just like the way it looks when reading the code. But, if this actually has an effect on the speed of the code, it's probably not a good idea. Does anyone know if extra brackets affects the speed? Thanks.

推荐答案

不是。

一般来说,规则,编译器有很多余地来优化东西。

No it does not.
In general, due to the "as if"-rule, the compiler has a lot of leeway to optimize things.

不过,这是一个风格问题,每个人都同意很少有直接的答案。

Still, it is a style issue, and there are seldom straight answers everyone agrees on.

有些人只使用任何类型的大括号,如果他们明确地澄清代码或必要的,那些总是使用代码块的条件和循环。 br>
如果你在团队/合同/继承的代码库上工作,尽量符合他们的风格,即使它不是你的风格。

There are people who only use braces of any kind if they either significantly clarify the code or are neccessary, and those who always use a code block for conditionals and loops.
If you work in a team/on contract/on an inherited codebase, try to conform to their style, even if it's not yours.

这篇关于在c ++程序中添加不必要的花括号{}会减慢它吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆