程序段计时操作,并将运行时间输出TXT文档

来源:互联网 发布:开机自动运行软件 编辑:程序博客网 时间:2024/06/06 10:46

int nCount = 0;void COperateFile::Tick(){LARGE_INTEGER litmp;LONGLONG Qpart1,Qpart2;double dMinus,dFreq,dTime;QueryPerformanceFrequency(&litmp);dFreq = (double)litmp.QuadPart;QueryPerformanceCounter(&litmp);Qpart1 = litmp.QuadPart;//do somethingSleep(1567);QueryPerformanceCounter(&litmp);Qpart2 = litmp.QuadPart;dMinus = (double)(Qpart2-Qpart1);dTime = dMinus/dFreq *1000; //ms,microSecond///dTime = dMinus/dFreq //s,secondchar lpSpendTime[256] = "\0";sprintf(lpSpendTime,"  Cost Time: %lf ms  %d NO",dTime,nCount++);//get system timeSYSTEMTIME systemTime;memset((void*)&systemTime,0,sizeof(SYSTEMTIME));GetSystemTime(&systemTime);char lpSystemTime[256] = "\0";sprintf(lpSystemTime,"[ %4d.%2d.%2d  %2d:%2d:%2d ]",systemTime.wYear,\systemTime.wMonth,systemTime.wDay,systemTime.wHour+8,\systemTime.wMinute,systemTime.wSecond);char lpMsg[512] = "\0";strcpy(lpMsg,lpSystemTime);strcat(lpMsg,lpSpendTime);strcat(lpMsg,"\n");//write message to txt filechar lpFileName[256] = "\0";strcpy(lpFileName,"D:\\Log\\test\\log.txt");FILE* pFile = fopen(lpFileName,"r");if (!pFile){pFile = fopen(lpFileName,"w");//create file if not existfputs(lpMsg,pFile);fclose(pFile);}else{fclose(pFile);pFile = fopen(lpFileName,"a");// add if file existfputs(lpMsg,pFile);fclose(pFile);}}

0 0
原创粉丝点击