在 C++ 中是否应该始终避免变量声明?
Should variable declarations always be avoided in C++?
有时为了清楚起见,我在代码中声明了 "one use" 个变量。这会显着影响性能还是编译器可以优化它?
例如,我倾向于这样做:
int minVal = long_arithmetic_expresion();
int maxVal = even_longer_expression();
for (int i = minVal; i < maxVal; i++)
{
// Do stuff not related to minVal or maxVal
}
double part1 = 4*sqrt(something)* ... // Very long thing
double part2 = 5*sqrt(something else)* ... // Very long thing
double interestingValue = part1 / part2; // This is the only interesting variable for later
而不是:
for (int i = long_arithmetic_expresion(); i < even_longer_expression(); i++)
{
// Do stuff not related to minVal or maxVal
}
double interestingValue = (4*sqrt(whatever)* ...) / (5*sqrt(something else)* ...);
这个 for 循环将包含在一个将被多次调用的函数中,因此即使是很小的性能提升也与我的情况相关。
注:
正如很快指出的那样,even_longer_expression() 有可能在循环的每一步都被评估,这当然不好.
为了清楚起见,我的问题与声明一次性变量的事实有关。
我在循环之后添加了更多代码。我指的是变量 part1 和 part2.
Does this affect dramatically the performance or can the compiler optimize it?
完全取决于:
如果 long_arithmetic_expresion()
和 even_longer_expression()
被标记为 constexpr
并且在运行时不太可能改变,编译器可以优化对这些函数的重复调用。
否则使用一次初始化的变量可能会更好
除非您禁用优化,否则以下代码几乎肯定会在现代编译器上显示出完全相同的性能(假设表达式显然是独立的):
// save to temporary minVal variable
int minVal = long_arithmetic_expresion();
int maxVal = even_longer_expression();
for (int i = minVal; i < maxVal; i++) {
...
}
// avoid creating temporary minVal variable
int maxVal = even_longer_expression();
for (int i = long_arithmetic_expresion(); i < maxVal; i++) {
...
}
但第一个版本通常更具可读性=)
原因是:copy propagation 对于基本类型的变量对于编译器来说是微不足道的。所以在第一个版本中,编译器会删除 i = minVal
赋值。
有时为了清楚起见,我在代码中声明了 "one use" 个变量。这会显着影响性能还是编译器可以优化它?
例如,我倾向于这样做:
int minVal = long_arithmetic_expresion();
int maxVal = even_longer_expression();
for (int i = minVal; i < maxVal; i++)
{
// Do stuff not related to minVal or maxVal
}
double part1 = 4*sqrt(something)* ... // Very long thing
double part2 = 5*sqrt(something else)* ... // Very long thing
double interestingValue = part1 / part2; // This is the only interesting variable for later
而不是:
for (int i = long_arithmetic_expresion(); i < even_longer_expression(); i++)
{
// Do stuff not related to minVal or maxVal
}
double interestingValue = (4*sqrt(whatever)* ...) / (5*sqrt(something else)* ...);
这个 for 循环将包含在一个将被多次调用的函数中,因此即使是很小的性能提升也与我的情况相关。
注:
正如很快指出的那样,even_longer_expression() 有可能在循环的每一步都被评估,这当然不好. 为了清楚起见,我的问题与声明一次性变量的事实有关。 我在循环之后添加了更多代码。我指的是变量 part1 和 part2.
Does this affect dramatically the performance or can the compiler optimize it?
完全取决于:
如果 long_arithmetic_expresion()
和 even_longer_expression()
被标记为 constexpr
并且在运行时不太可能改变,编译器可以优化对这些函数的重复调用。
否则使用一次初始化的变量可能会更好
除非您禁用优化,否则以下代码几乎肯定会在现代编译器上显示出完全相同的性能(假设表达式显然是独立的):
// save to temporary minVal variable
int minVal = long_arithmetic_expresion();
int maxVal = even_longer_expression();
for (int i = minVal; i < maxVal; i++) {
...
}
// avoid creating temporary minVal variable
int maxVal = even_longer_expression();
for (int i = long_arithmetic_expresion(); i < maxVal; i++) {
...
}
但第一个版本通常更具可读性=)
原因是:copy propagation 对于基本类型的变量对于编译器来说是微不足道的。所以在第一个版本中,编译器会删除 i = minVal
赋值。