精确计算代码段执行时间
2010-12-24 03:50
330 查看
#include <cstdio> #include <Windows.h> #include <atlbase.h> #include <Tlhelp32.h> #include <time.h> //........... int WINAPI findString(int lpmem,int memsize,int lpString,int StringSize,int AddrList)//; // { int nRet=-1; __asm { pushad; mov edx,lpmem; add edx,memsize; sub edx,StringSize; mov edi,lpmem; mov esi,lpString; mov eax,[esi]; mov ecx,[esi+4]; mov ebx,AddrList; LableBegin: cmp eax,[edi]; jnz LableNext; cmp ecx,[edi+4]; jnz LableNext; mov ecx,StringSize; push edi; repz cmpsb; pop edi; mov esi,lpString; mov ecx,[esi+4]; jnz LableNext; LableSave: mov ebx,edi; add ebx,4; LableNext: inc edi; cmp edi,edx; jnz LableBegin; LableRet: mov nRet,ebx; popad; } return nRet; } //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> int SearchByteShuZu(byte * bForSearch,int ifLen, byte* bWaitSearch, int iwLen,int iFirstSearchPos) {//int SearchByteShuZu(byte * bForSearch,int ifLen, byte* bWaitSearch, int iwLen,int iFirstSearchPos);//寻找字节集数组(待寻找的,待寻找的最大长度,要寻找的,要寻找的长度,起始寻找位置) 返回-1 为未找到 位置从0开始 int iCurrentPos=0;//待搜索位置 if (iFirstSearchPos>ifLen)//超过待搜索的长度 { return -1; } if (iwLen>ifLen)//要搜索的长度比 待搜索的长度大 返回 { return -1; } if (iwLen==0||ifLen==0) { return -1;//0 返回 } // bForSearch+=iFirstSearchPos;//设置起始搜索位置 iCurrentPos+=iFirstSearchPos;//设置起始搜索位置 bForSearch+=iCurrentPos;//设置指针 对应 while(1) { while(*bForSearch!=*bWaitSearch)//找到第一个相同的 { iCurrentPos+=1;//当前搜索位置前进1 bForSearch+=1;//当前指针前进1 if (iCurrentPos>ifLen) //长度超过待搜索的整体长度返回 { return -1; } } for (int i=0;i<iwLen;++i)//开始对比 { if (*(bForSearch+i)!=*(bWaitSearch+i)) { iCurrentPos+=1;//当前搜索位置前进1 bForSearch+=1;//当前指针前进1 //设置下指针前进1 break;//找到一个不同的 跳出 } if (i+1==iwLen) { return iCurrentPos;//找到列 } } } } __int64 FileTimeToQuadWord(PFILETIME pft) { return Int64ShllMod32(pft->dwHighDateTime,32)|pft->dwLowDateTime; } __int64 GetThreadRunTime() //取线程当前占用cpu时间 单位:100ns(纳秒) { FILETIME ftKernelTime,ftUserTime,ftDummy; GetThreadTimes(GetCurrentThread(),&ftDummy,&ftDummy,&ftKernelTime,&ftUserTime); return FileTimeToQuadWord(&ftKernelTime)+FileTimeToQuadWord(&ftUserTime); } char szFind[0x20000000]; int main() { LPSTR szsea="45644564"; __int64 qwStartTime=0,qwTime=0; qwStartTime=GetThreadRunTime();//setvalue srand(time(NULL)); for (int i=0;i<sizeof(szFind)/sizeof(DWORD);i++) { ((DWORD*)szFind)[i]=rand(); } qwTime=GetThreadRunTime()-qwStartTime; printf("/r/nSetValue:%I64d*100ns %dms/r/n",qwTime,qwTime/1000/10); qwStartTime=GetThreadRunTime(); findString((int)szFind,sizeof(szFind),(int)szsea,8,0); qwTime=GetThreadRunTime()-qwStartTime; printf("/r/nfindString:%I64d*100ns %dms/r/n",qwTime,qwTime/1000/10); qwStartTime=GetThreadRunTime(); SearchByteShuZu((byte*)szFind,sizeof(szFind),(byte*)szsea,8,0); qwTime=GetThreadRunTime()-qwStartTime; printf("/r/nSearchByteShuZu:%I64d*100ns %dms/r/n",qwTime,qwTime/1000/10); return 0; }
GetThreadTimes 获取的时间只是cpu执行时间 不包含睡眠时间及等待事件的时间
效率对比用
int memfind(const char *mem, const char *str, int sizem, int sizes) { int da,i,j; if (sizes == 0) da = strlen(str); else da = sizes; for (i = 0; i < sizem; i++) { for (j = 0; j < da; j ++) if (mem[i+j] != str[j]) break; if (j == da) return i; } return -1; }
相关文章推荐
- 测试代码段的执行时间并精确到微秒
- Linux学习2之shell脚本计算代码段运行的时间(精确到毫秒)
- C++精确计算代码执行时间精度微秒级
- 精确计算代码执行时间
- 精确计算代码执行时间
- 计算代码段的执行时间
- 精确计算代码执行时间
- [iuud8]cocos2dx编程中获得代码段执行时间,精确到毫秒级
- VC精确计算代码执行时间
- CCS中使用DSP定时器的计时程序,可以精确计算某一段程序的执行时间
- C++精确计算代码执行时间精度微秒级
- 精确计算代码执行时间
- VC精确计算代码执行时间方法
- asp.net2中精确计算页面执行时间原来如此简单!
- QueryPerformanceCounter精确计算执行时间
- c#中精确计算程序执行时间
- 如何精确计算一个方法执行的时间-如博客园的提交评论提交耗时xxx毫秒?
- asp.net2中精确计算页面执行时间原来如此简单!
- 精确计算代码执行时间
- 计算php代码执行时间长短的类(精确到毫秒)