您的位置:首页 > 其它

定义一个计算操作时间的宏

2008-08-06 20:30 267 查看
#include "stdafx.h"

#include <Windows.h>
#include <string>
using namespace std;

void InfoLog( string info_log ) // 记录信息
{
FILE * pTargetFile = NULL;
pTargetFile = fopen( "LogInfo.txt", "a+" );

if ( pTargetFile != NULL )
{
fwrite( info_log.c_str(), strlen( info_log.c_str() ), 1, pTargetFile );
fclose( pTargetFile );
}
}

// 计算时间的宏
#define COMPUTE_TIME(OPERATOR) /
{/
int start = GetTickCount(); /
{OPERATOR} /
int stop = GetTickCount();/
string info; /
char szTemp[32]; /
memset(szTemp, 0, 32); /
info = ""#OPERATOR" "; /
info += "time cost: "; /
info += itoa(stop-start, szTemp, 10); /
info += "ms/n"; /
InfoLog(info);/
}

// 使用测试
int _tmain(int argc, _TCHAR* argv[])
{
COMPUTE_TIME( //括号里面的操作被计时
Sleep(1000);
for (int a=0; a<1000; a++);
int stop = 34; )

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐