简体   繁体   English

我应该担心多余的声明吗?

[英]Should I be concerned about redundant declarations?

for(int i = 0; i < 10; ++i){
    int x = 0;
    //stuff
}

vs对比

int x;
for(int i = 0; i < 10; ++i){
    x = 0;
    //stuff
}

I'm concerned about performance and memory usage.我担心性能和内存使用情况。

Modern compilers do a lot of optimizations under the hood.现代编译器在幕后做了很多优化。 You might feel that the first example is not performant because it creates x for each iteration, however it is not the case.您可能会觉得第一个示例性能不佳,因为它为每次迭代都创建了x ,但事实并非如此。

First example: https://godbolt.org/z/T2bsvG第一个例子: https : //godbolt.org/z/T2bsvG

Second example: https://godbolt.org/z/sRNjcV第二个例子: https : //godbolt.org/z/sRNjcV

If you compile the assembly, you can see they are identical.如果编译程序集,您可以看到它们是相同的。

Should I be concerned about redundant declarations?我应该担心多余的声明吗?

Usually, no.通常,没有。

I'm concerned about performance and memory usage.我担心性能和内存使用情况。

You can compare the assembly of one way to the other, and may find that the resulting program is identical.您可以将一种方式的程序集与另一种方式进行比较,可能会发现生成的程序是相同的。 If not, then you can measure the performance, if that is your concern.如果没有,那么您可以衡量性能,如果这是您的关注点。 But I suspect that there is probably no difference in performance in practice.但我怀疑在实践中可能没有性能差异。

Limiting the scope of variables to be as narrow as is sufficient is usually the best practice for making the program easy to understand.将变量的范围限制为足够窄通常是使程序易于理解的最佳实践。

If you're asking this in terms of scoping then yes there are times (especially when dealing with smart pointers ) when it is important whether to have a variable live in the scope where it is used or have it live outside that scope.如果您在范围界定方面问这个问题,那么是的,有时(尤其是在处理智能指针时)重要的是让变量存在于使用它的范围内还是存在于该范围之外。

As you may know, the reason for having smart pointers in C++ is to aid in resource management so that freeing heap memory does not have to be a chore.您可能知道,在 C++ 中使用智能指针的原因是帮助资源管理,以便释放堆内存不必是一件苦差事。

See: What is a smart pointer and when should I use one?请参阅: 什么是智能指针,何时应该使用?

An example could be if you had smart pointer that is used to encapsulate a reference to a database connection and you have to call a function that uses this connection.例如,如果您有用于封装对数据库连接的引用的智能指针,并且您必须调用使用此连接的函数。 It would really matter where that smart pointer lives because if it lives inside the function, then the connection would be opened and closed every time this function is called which could cost you precious lost milliseconds and depending on the frequency of the calls, it could end up costing several hours in lost time.智能指针所在的位置真的很重要,因为如果它位于函数内部,那么每次调用此函数时都会打开和关闭连接,这可能会花费您宝贵的毫秒数,并且根据调用的频率,它可能会结束浪费了几个小时的时间。 However, if it lives outside the function then the database is already opened whenever the function is called and there is no need to open it again.但是,如果它位于函数之外,那么只要调用该函数,数据库就已经打开,无需再次打开它。

My rule of thumb is to try as much as possible to delay creating any variables up until the point where it is needed.我的经验法则是尽量推迟创建任何变量,直到需要它为止。 Undoubtedly, there are cases when it doesn't really matter such as the case you have above, and there are people who like the aesthetics of code that declares variables in one place, and then use them.毫无疑问,有些情况并不重要,例如你上面的情况,有些人喜欢在一个地方声明变量然后使用它们的代码美学。

In terms of performance, rather than int x = 0;在性能方面,而不是int x = 0; consider MyType x = 0;考虑MyType x = 0; where the constructor for MyType does a lot of work. MyType的构造函数在这里做了很多工作。 In that case, creating x once before entering the loop will do less work than creating x each time through the loop.在这种情况下,在进入循环之前创建x一次将比每次通过循环创建x做的工作更少。 However, if the logic of the program requires a new x each time through the loop, creating a single one before entering the loop will simply be wrong.但是,如果程序的逻辑在每次循环中都需要一个新的x ,那么在进入循环之前创建单个x将是错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM