C++中使用巨集定義來註釋掉所有的cout輸出
對於大一點的程式,為了驗證正確性,除了看直接的效果外,裡面還會增加一系列的cout輸出。在除錯過程中,也會增加一系列的輸出,但是當程式量大的時候,這些輸出的清楚將會變成很麻煩的事情。如何通過巨集定義依次註釋掉所有的輸出呢?本文給了嘗試。
直接上程式碼:
#include <iostream>
#define TRACE
#ifndef TRACE
#define tcout 0 && cout//或者NULL && cout
#else
#define tcout cout
#endif
using namespace std;
int main(){
tcout<<"abdsa"<<endl;
return 0;
}
#include <iostream>
//#define TRACE
#ifndef TRACE
#define tcout 0 && cout
#else
#define tcout cout
#endif
using namespace std;
int main(){
tcout<<"abdsa"<<endl;
return 0;
}
簡單吧。知其然而知其所以然,為啥呀?我們把tcout打出來,
#include <iostream>
//#define TRACE
#ifndef TRACE
#define tcout 0 && cout //或者NULL && cout
#else
#define tcout cout
#endif
using namespace std;
int main(){
printf("%p\n",tcout);
tcout<<"abdsa"<<endl;
return 0;
}
發現0 &&cout計算後為nil。
#include <iostream>
#define TRACE
#ifndef TRACE
#define tcout NULL && cout
#else
#define tcout cout
#endif
using namespace std;
int main(){
//printf("%p\n",tcout);
0 && tcout<<"abdsa"<<endl;
return 0;
}
可以看到我們把0 &&挪到輸出前面,效果是一樣的,這說明cout其實並不是沒有被呼叫,只不過是執行完之後沒有輸出到console上來,因為把輸出到console緩衝區的內容和0或者NULL作了&&操作,才不輸出的。
但是這個是不是有開銷,我就不清楚了。如果有開銷,那麼這種並不是真正的解決方案,如果沒有開銷,或者說並沒有嘗試去將內容寫向console,那就可以作為解決方案。
我們再測試一下具體的時間:
(1)列印資料出來:耗時155ms左右
#include <iostream>
#include <sys/time.h>
#include <unistd.h>
#define TRACE
#ifndef TRACE
#define tcout NULL && cout
#else
#define tcout cout
#endif
using namespace std;
int main(){
struct timeval timeStart,timeEnd;
gettimeofday(&timeStart, NULL );
long runTime = timeStart.tv_sec*1000000 +timeStart.tv_usec;
//printf("%p\n",tcout);
for(int i=0;i<100000;i++)
{
//0 && tcout<<"abdsa"<<endl;
tcout<<"abdsa"<<endl;
}
gettimeofday(&timeEnd, NULL );
runTime = timeEnd.tv_sec*1000000 +timeEnd.tv_usec - runTime;
printf("time consume: %ld\n",runTime);
return 0;
}
(2)使用0 && cout,耗時721us
#include <iostream>
#include <sys/time.h>
#include <unistd.h>
#define TRACE
#ifndef TRACE
#define tcout NULL && cout
#else
#define tcout cout
#endif
using namespace std;
int main(){
struct timeval timeStart,timeEnd;
gettimeofday(&timeStart, NULL );
long runTime = timeStart.tv_sec*1000000 +timeStart.tv_usec;
//printf("%p\n",tcout);
for(int i=0;i<100000;i++)
{
0 && tcout<<"abdsa"<<endl;
//tcout<<"abdsa"<<endl;
}
gettimeofday(&timeEnd, NULL );
runTime = timeEnd.tv_sec*1000000 +timeEnd.tv_usec - runTime;
printf("time consume: %ld\n",runTime);
return 0;
}
(3)測試不列印任何輸出的情況下的耗時:737us
#include <iostream>
#include <sys/time.h>
#include <unistd.h>
#define TRACE
#ifndef TRACE
#define tcout NULL && cout
#else
#define tcout cout
#endif
using namespace std;
int main(){
struct timeval timeStart,timeEnd;
gettimeofday(&timeStart, NULL );
long runTime = timeStart.tv_sec*1000000 +timeStart.tv_usec;
//printf("%p\n",tcout);
for(int i=0;i<100000;i++)
{
//0 && tcout<<"abdsa"<<endl;
//tcout<<"abdsa"<<endl;
}
gettimeofday(&timeEnd, NULL );
runTime = timeEnd.tv_sec*1000000 +timeEnd.tv_usec - runTime;
printf("time consume: %ld\n",runTime);
return 0;
}
這說明,上述方法是一個可使用的解決方案,不會降低程式的效能。
如果要控制日誌的輸出,就用:
#ifndef TRACE
#define tcout 0 && Loger::instance()::log
#else
#define tcout Loger::instance()::log
#endif
相關文章
- c++ cout 格式輸出C++
- C++中巨集定義#define的用法C++
- C++巨集定義#defineC++
- C++輸入輸出常用格式(cin,cout,stringstream)C++
- C/C++中printf和C++中cout的輸出格式C++
- c/c++標準預定義巨集C++
- C++輸出流cout的執行順序問題C++
- 使用ant巨集定義任務
- C/C++語言巨集定義##連線符和符#的使用C++
- C語言巨集定義中#define中的井號#的使用C語言
- C++初始化,之不明白篇 cout<<x<<endl 與 cout<<"x = "<<cout<<x<<endl的輸出的值會不一樣C++
- C++ | VS2017 C++專案配置使用的常見巨集定義C++
- 0x03. 使用巨集定義事件事件
- iOS常用巨集定義iOS
- 一個C++巨集定義與列舉定義重複的編譯錯誤C++編譯
- 【C進階】21、巨集定義與使用分析
- C語言(巨集定義)C語言
- Swift 裡的巨集定義Swift
- 教你看懂C++類庫函式定義之一---HRESULT 巨集C++函式
- iOS開發:pch檔案中的巨集定義iOS
- 記憶體對齊巨集定義的簡明解釋記憶體
- iOS標頭檔案中如何在巨集裡引用已有的巨集iOS
- iOS 通用巨集定義 高效全域性巨集彙總iOS
- std::cout 輸出 unsigned char型別資料型別
- ios開發的巨集定義iOS
- VC++實用巨集定義C++
- iOS開發技巧:應用巨集定義使用字型iOS
- iOS 官方巨集定義 - “引入”、“廢棄”iOS
- C語言中的標頭檔案中的巨集定義C語言
- libev中ev_loop結構體中巨集定義的理解OOP結構體
- C++中的檔案輸入/輸出(3):掌握輸入/輸出流 (轉)C++
- C語言巨集定義##連線符和#符的使用C語言
- MongoDB db.serverStatus()輸出內容中文註釋MongoDBServer
- 【C++】輸入輸出C++
- 在C++ Builder中定義事件 (轉)C++UI事件
- iOS-日常開發常用巨集定義iOS
- 059.Define 巨集定義的巢狀巢狀
- define巨集定義和const常量定義之間的區別