转自:http://blog.21ic.com/user1/69/archives/2006/13695.html
宏中"#"和"##"的用法
1、一般用法
使用#把宏参数变为一个字符串,用##把两个宏参数贴合在一起。
2、当宏参数是另一个宏的时候
凡宏定义里有用'#'或'##'的地方宏参数是不会再展开。
更多的详细用法,请参见原作者博客。
下面附上OpenCV中好用的计时宏:
转自:http://www.aishack.in/2010/08/timing-macros-in-c/
// Record the execution time of some code, in milliseconds. #define DECLARE_TIMING(s) int64 timeStart_##s; double timeDiff_##s; double timeTally_##s = 0; int countTally_##s = 0 #define START_TIMING(s) timeStart_##s = cvGetTickCount() #define STOP_TIMING(s) timeDiff_##s = (double)(cvGetTickCount() - timeStart_##s); timeTally_##s += timeDiff_##s; countTally_##s++ #define GET_TIMING(s) (double)(timeDiff_##s / (cvGetTickFrequency()*1000.0)) #define GET_AVERAGE_TIMING(s) (double)(countTally_##s ? timeTally_##s/ ((double)countTally_##s * cvGetTickFrequency()*1000.0) : 0) #define CLEAR_AVERAGE_TIMING(s) timeTally_##s = 0; countTally_##s = 0
这些宏定义使用了cvGetTickCount()和cvGetTickFrequency()方法,故他们依赖于OpenCV,仅能用于视觉类的应用程序。
其使用方法非常简单:
例如:DECLARE_TIMING(TTimer);
while(1) {
START_TIMING(TTimer); cvThreshold(channelRed, channelRed, 20, 255, CV_THRESH_BINARY); STOP_TIMING(TTimer); printf("Execution time: %4.3f ms./n", GET_TIMING(TTimer));
...
}
printf("Average Time: %4.3f ms per iteration./n", GET_AVERAGE_TIMING(TTimer));