cout或printf是调试的常用手段,客户端对性能要求通常不及服务器端,服务器生产环境,尽量避免频繁打印。否则影响性能。
举例:
#include <iostream>
#include <time.h>
using namespace std;
int main()
{
long sTime, eTime, timeForCout, timeForPrintf;
int a[30000];
int i;
for (i = 0; i < 30000; i++) {
a[i] = i;
}
sTime = clock();
for (i = 0; i < 30000; i++) {
cout << a[i] << "\n";
}
eTime = clock();
timeForCout = eTime - sTime;
sTime = clock();
for (i = 0; i < 30000; i++) {
printf("%d\n", a[i]);
}
eTime = clock();
timeForPrintf = eTime - sTime;
cout << "cout时间:" << timeForCout << "ms" << endl;
cout << "printf时间:" << timeForPrintf << "ms" << endl;
system("pause");
return 0;
}
第一次执行结果:
cout时间:74237ms
printf时间:58859ms
第一次执行结果:
cout时间:56797ms
printf时间:22248ms