寫這篇是因為在問答平台看到一個問題:C++ 內層循環中定義變數和在外面定義,影響大嗎?
例如:
for(int i=0;i<999;i++) { for(int j=0;j<999;j++); }內層循環每次都定義 j,會造成多大的消耗呢?
我當時的回答是:這取決於你用的編譯器,不過主流編譯器(如 vs 與 gcc)在這塊優化都做得不錯,不會反覆配置變數。既然很多人有興趣,那就不如實測分享。
#include <cstdio>
using namespace std;
void Test1() {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
printf("%d,%d\n", int(i), int(j));
}
}
}
void Test2() {
int i, j;
for (i = 0; i < 2; i++) {
for (j = 0; j < 3; j++) {
printf("%d,%d\n", int(i), int(j));
}
}
}
int main() {
Test1();
Test2();
return 0;
}
兩者做完全相同的事,唯一差別是 j 定義在循環外還是循環內。用最低優化等級 O0 編譯:
g++ -O0 -g test.cpp
反組譯 Test1:
(gdb) disas /m Test1
Dump of assembler code for function Test1():
5 {
0x0804841d <+0>: push %ebp
0x0804841e <+1>: mov %esp,%ebp
0x08048420 <+3>: sub $0x28,%esp
6 for (int i = 0; i < 2; i++)
0x08048423 <+6>: movl $0x0,-0x10(%ebp)
0x0804842a <+13>: jmp 0x804845d <Test1()+64>
0x08048459 <+60>: addl $0x1,-0x10(%ebp)
0x0804845d <+64>: cmpl $0x1,-0x10(%ebp)
0x08048461 <+68>: jle 0x804842c <Test1()+15>
7 {
8 for (int j = 0; j < 3; j++)
0x0804842c <+15>: movl $0x0,-0xc(%ebp)
0x08048433 <+22>: jmp 0x8048453 <Test1()+54>
0x0804844f <+50>: addl $0x1,-0xc(%ebp)
0x08048453 <+54>: cmpl $0x2,-0xc(%ebp)
0x08048457 <+58>: jle 0x8048435 <Test1()+24>
10 printf("%d,%d\n", int(i), int(j));
0x08048435 <+24>: mov -0xc(%ebp),%eax
0x08048438 <+27>: mov %eax,0x8(%esp)
0x0804843c <+31>: mov -0x10(%ebp),%eax
0x0804843f <+34>: mov %eax,0x4(%esp)
0x08048443 <+38>: movl $0x8048560,(%esp)
0x0804844a <+45>: call 0x80482f0 <printf@plt>
11 }
12 }
14 }
0x08048463 <+70>: leave
0x08048464 <+71>: ret
End of assembler dump.
在內層 for (int j = 0; j < 3; j++) 底下,完全看不到配置變數 j 的組語指令。把 Test1 與 Test2 的組語並排一比,你會發現兩段指令一模一樣(Test2 的位址從 0x08048465 起,其餘結構逐行對應)。結論:用 gcc 編譯時,你完全不必糾結變數定義在循環內外,效果相同——為了程式碼美觀,寫在循環內即可。vs 的效果讀者可自行驗證。
把 int 換成自訂類別,並在構造函數裡加印 statement 以便觀察物件建立時機:
#include <cstdio>
using namespace std;
class MyInt {
public:
MyInt(int i): m_iValue(i) { printf("Constructed: MyInt(%d)\n", i); }
MyInt() { printf("Constructed: MyInt()\n"); }
MyInt &operator++(int i) { m_iValue ++; return *this; }
bool const operator <(const MyInt& another) { return m_iValue < another.m_iValue; }
operator int() { return m_iValue; }
MyInt &operator =(int i) { m_iValue = i; return *this; }
private:
int m_iValue;
};
void Test1() {
for (MyInt i = MyInt(0); i < MyInt(2); i++) {
for (MyInt j = MyInt(0); j < MyInt(3); j++) {
printf("%d,%d\n", int(i), int(j));
}
}
}
void Test2() {
MyInt i, j;
for (i = MyInt(0); i < MyInt(2); i++) {
for (j = MyInt(0); j < MyInt(3); j++) {
printf("%d,%d\n", int(i), int(j));
}
}
}
void Test3() {
MyInt i, j;
for (i = 0; int(i) < 2; i++) {
for (j = 0; int(j) < 3; j++) {
printf("%d,%d\n", int(i), int(j));
}
}
}
int main() {
printf("Test1---------------------------------\n"); Test1();
printf("Test2---------------------------------\n"); Test2();
printf("Test3---------------------------------\n"); Test3();
return 0;
}
同樣用 g++ -O0 編譯執行,輸出如下:
Test1--------------------------------- Constructed: MyInt(0) Constructed: MyInt(2) Constructed: MyInt(0) Constructed: MyInt(3) 0,0 Constructed: MyInt(3) 0,1 Constructed: MyInt(3) 0,2 Constructed: MyInt(3) Constructed: MyInt(2) Constructed: MyInt(0) Constructed: MyInt(3) 1,0 Constructed: MyInt(3) 1,1 Constructed: MyInt(3) 1,2 Constructed: MyInt(3) Constructed: MyInt(2) Test2--------------------------------- Constructed: MyInt() Constructed: MyInt() Constructed: MyInt(0) Constructed: MyInt(2) Constructed: MyInt(0) Constructed: MyInt(3) 0,0 Constructed: MyInt(3) 0,1 Constructed: MyInt(3) 0,2 Constructed: MyInt(3) Constructed: MyInt(2) Constructed: MyInt(0) Constructed: MyInt(3) 1,0 Constructed: MyInt(3) 1,1 Constructed: MyInt(3) 1,2 Constructed: MyInt(3) Constructed: MyInt(2) Test3--------------------------------- Constructed: MyInt() Constructed: MyInt() 0,0 0,1 0,2 1,0 1,1 1,2
可以清楚看到 Test3 建立物件的次數最少。分析:
i = MyInt(0) 與判斷條件 i < MyInt(2) 仍會構造暫時物件,所以循環中還是有多次配置。operator= 直接以整數賦值,避免賦值句中建立 MyInt;並用 int(i) < 2、int(j) < 3 避免判斷條件中建立物件,因此整段只在循環外配置兩次變數,是最高效的寫法。