关于cout函数和printf函数的效率问题
写了个小程序测试了一下cout函数和printf函数的效率,看来printf函数的确效率要高不少。
1 #include <iostream> 2 #include <time.h> 3 using namespace std; 4 5 int main() 6 { 7 long sTime, eTime, timeForCout, timeForPrintf; 8 int a[30000]; 9 10 int i; 11 for (i = 0; i < 30000; i++) { 12 a[i] = i; 13 } 14 15 sTime = clock(); 16 for (i = 0; i < 30000; i++) { 17 cout << a[i] << "\n"; 18 } 19 eTime = clock(); 20 timeForCout = eTime - sTime; 21 22 sTime = clock(); 23 for (i = 0; i < 30000; i++) { 24 printf("%d\n", a[i]); 25 } 26 eTime = clock(); 27 timeForPrintf = eTime - sTime; 28 29 cout << "cout时间:" << timeForCout << "ms" << endl; 30 cout << "printf时间:" << timeForPrintf << "ms" << endl; 31 32 system("pause"); 33 return 0; 34 }