简体   繁体   English

如果在编译期间不知道局部变量const的值,则在C ++中声明局部变量const的好处

[英]Benefit of declaring a local variable const in C++, if its value is not known during compile time

I am currently working in a piece of software, which is highly performance critical, so each optimization counts for me. 我目前正在使用一款对性能至关重要的软件,因此每次优化对我来说都很重要。 There is one critical situation that very frequently occurs inside a loop in which I calculate two int indices of a std::vector by using double (for clarification, convert metric positions to map-positions). 在循环中经常发生一种紧急情况,在该循环中,我通过使用double计算std::vector的两个int索引(为清楚起见,将度量标准位置转换为map-positions)。

There are three sensible ways to do that in my opinion. 我认为有三种明智的方法可以做到这一点。

//first possibility

int indexX, indexY;
for(int x = 0; x <= xMax; ++x)
{
  for(int y = 0; y <= yMax; ++y)
  {
    indexX = //calculate value using x somehow
    indexY = //calculate value using y somehow
    //do multiple things with indexX and indexY 
  }
}

//second possibility

for(int x = 0; x <= xMax; ++x)
{
  for(int y = 0; y <= yMax; ++y)
  {
    int indexX = //calculate value using x somehow
    int indexY = //calculate value using y somehow
    //do multiple things with indexX and indexY 
  }
}

//third possibility

for(int x = 0; x <= xMax; ++x)
{
  for(int y = 0; y <= yMax; ++y)
  {
    const int indexX = //calculate value using x somehow
    const int indexY = //calculate value using y somehow
    //do multiple things with indexX and indexY 
  }
}

After some search on SO, people generally seem to not recommend the first case and say that better optimizations are possible, if variables are declared as locally as possible. 在对SO进行了一些搜索之后,人们似乎通常不建议第一种情况,并说,如果将变量声明为尽可能局部的,则可以进行更好的优化。 I have tested that and it seems to be correct so far, IF optimizations are turned on during compiling. 我已经测试过了,到目前为止似乎是正确的,在编译过程中启用了IF优化。

However, I am not sure about cases 2/3. 但是,我不确定情况2/3。 All topics on const I could find on SO concern using the keyword as a modifier for function parameters, rather than local variables. 使用关键字作为函数参数的修饰符而不是局部变量的修饰符,我可以在const找到与const有关的所有主题。 Sell me on const correctness is a very general discussion on the topic and mostly deals with "accidental error protection". 向我介绍const正确性是关于该主题的非常笼统的讨论,并且主要涉及“意外错误保护”。 The accepted answer also states compiler optimizations "are possible", but I could not observe any performance differences in my case. 可接受的答案还指出了“可能”进行编译器优化,但是在我的案例中我看不到任何性能差异。

I understand that the compiler will most likely convert something like const int number = 5 to the actual number (as stated here , it's C#, but I don't expect it to differ for C++) however, in my case it is not known during compile time. 据我所知,编译器很可能会像转换const int number = 5的实际数量(如说在这里 ,这是C#,但我不希望它对于C不同++),但是,在我的情况下,它不会在知编译时间。

Does the compiler detect that a local variable is only assigned to once and is thus guaranteed to treat both cases the same? 编译器是否检测到局部变量只分配了一次,从而保证将两种情况都相同? Could it be that one might lead to better optimizations than the other? 难道一个人可能会比另一个人带来更好的优化? Are those optimizations always better for one case than the other or could it switch between the two? 在一种情况下,这些优化是否总是总是比另一种更好?还是可以在两种情况之间切换? Might it depend on the platform? 可能取决于平台吗?

Edit: I should mention. 编辑:我应该提到。 The code WILL be compiled on "highly different platforms" and I unfortunately, I cannot inspect the assembly outcome in most cases. 该代码将在“高度不同的平台”上编译,但不幸的是,在大多数情况下,我无法检查汇编结果。

I'm sorry because it is likely not to be the expected answer, but if the code WILL be compiled on "highly different platforms" , you should not even try to do such low level optimization. 很抱歉,因为这可能不是预期的答案,但是如果代码将在“高度不同的平台”上编译 ,则您甚至不应尝试进行此类低级优化。 As usual, carefully review all algorithms, profile the code (on one platform...) to see where you spend most of the time, and review twice those parts. 像往常一样,仔细检查所有算法,在一个平台上分析代码(…),以查看您大部分时间在哪里,并对这些部分进行两次检查。 If it is still not enough,write directly in assembly (one per supported target platform) the most critique parts. 如果还不够,请直接在装配体(每个受支持的目标平台一个)中编写评论最多的零件。 But for questions similar to yours, I now assume that the compiler will be smarter than a C++ programmer... 但是对于与您类似的问题,我现在假设编译器比C ++程序员更聪明...

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

相关问题 为什么 c++ 编译器不替换对 const class 成员的访问,其值在编译时已知? - Why don't c++ compilers replace this access to a const class member with its value known at compile time? 是否可以检查编译时是否知道const值? - Is it possible to check if const value is known at compile time? 通过从文件中读取其值来初始化 const 变量(C++) - Initializing a const variable by reading its value from file (C++) 专门化变量的值在编译时是否已知/未知 - Specialize if value of a variable is known/unknown at compile time 如何在编译期间定义 C++ 预处理器指令的值? - How to define value for C++ preprocessor directives during compile time? 当整数变量用于在C ++中声明数组大小时,错误显示为“表达式必须具有const值” - Error shows as “Expression must have a const value” when integer variable is used for declaring array size in c++ C ++返回局部变量的const引用 - c++ returning const reference of local variable 在c ++中声明const BYTE * - Declaring a const BYTE * in c++ 在函数内部声明const而不是变量有什么好处吗? - Is there any benefit to declaring a const rather than a variable inside a function? C ++中vlas的编译时与运行时const变量分配和分配 - Compile- vs run-time const variable assignment and allocation of vlas in C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM