ISDIGIT算法问题:两次比较与表查找比较谁更快

来源:互联网 发布:华泰证券网络培训学院 编辑:程序博客网 时间:2024/05/16 14:52

在《C++实践之路》[美]Barotsz Milewski书3.5.5中说,表查找比两次比较更快,实测不然,表查找略为更慢一些。

#define ISDIGIT2(c) ( ((c) >= '0') )   // 124800
#define ISDIGIT(c) ( ((c) >= '0') && ((c) <= '9') )//1404000 
if (bDig[UCHAR(n)]) isd+=2;     //1560010 表查找
if (isdigit(UCHAR(n))) isd+=2;    //4056026 MSVCR100.isdigit


全例如下:

int _tmain(int argc, _TCHAR* argv[]){srand(::GetTickCount());BOOL bDig[0xff] ={0};WCHAR szw[MAX_PATH];CHAR sza[MAX_PATH]; FILETIME lpCreationTime,  lpExitTime,   lpKernelTime, lpUserTime;FILETIME lpCreationTime2,  lpExitTime2,   lpKernelTime2, lpUserTime2;int isd=0;::GetThreadTimes(::GetCurrentThread(), &lpCreationTime,  &lpExitTime,   &lpKernelTime, &lpUserTime);isd=0;#define ISDIGIT2(c) ( ((c) >= '0') )//124800for (ULONG_PTR n=0; n <0x3ffffff; n++){if (ISDIGIT2(UCHAR(n))) isd+=2;}::GetThreadTimes(::GetCurrentThread(), &lpCreationTime2,  &lpExitTime2,   &lpKernelTime2, &lpUserTime2);swprintf(szw, __FUNCTIONW__ L" %d,\t%d, \n", isd, lpKernelTime2.dwLowDateTime -lpKernelTime.dwLowDateTime + (lpUserTime2.dwLowDateTime -lpUserTime.dwLowDateTime ));wprintf(szw);::GetThreadTimes(::GetCurrentThread(), &lpCreationTime,  &lpExitTime,   &lpKernelTime, &lpUserTime);isd=0;#define ISDIGIT(c) ( ((c) >= '0') && ((c) <= '9') )//1404000for (ULONG_PTR n=0; n <0x3ffffff; n++){if (ISDIGIT(UCHAR(n))) isd+=2;}::GetThreadTimes(::GetCurrentThread(), &lpCreationTime2,  &lpExitTime2,   &lpKernelTime2, &lpUserTime2);swprintf(szw, __FUNCTIONW__ L" %d,\t%d, \n", isd, lpKernelTime2.dwLowDateTime -lpKernelTime.dwLowDateTime + (lpUserTime2.dwLowDateTime -lpUserTime.dwLowDateTime ));wprintf(szw);::GetThreadTimes(::GetCurrentThread(), &lpCreationTime,  &lpExitTime,   &lpKernelTime, &lpUserTime);isd=0;for (ULONG_PTR n=0; n <0x3ffffff; n++)if (bDig[UCHAR(n)]) isd+=2;//1560010::GetThreadTimes(::GetCurrentThread(), &lpCreationTime2,  &lpExitTime2,   &lpKernelTime2, &lpUserTime2);swprintf(szw, __FUNCTIONW__ L" %d,\t%d, \n",  isd, lpKernelTime2.dwLowDateTime -lpKernelTime.dwLowDateTime + (lpUserTime2.dwLowDateTime -lpUserTime.dwLowDateTime ));wprintf(szw);::GetThreadTimes(::GetCurrentThread(), &lpCreationTime,  &lpExitTime,   &lpKernelTime, &lpUserTime);isd=0;for (ULONG_PTR n=0; n <0x3ffffff; n++)if (isdigit(UCHAR(n))) isd+=2;//4056026MSVCR100.isdigit ::GetThreadTimes(::GetCurrentThread(), &lpCreationTime2,  &lpExitTime2,   &lpKernelTime2, &lpUserTime2);swprintf(szw, __FUNCTIONW__ L" %d,\t%d, \n", isd, lpKernelTime2.dwLowDateTime -lpKernelTime.dwLowDateTime + (lpUserTime2.dwLowDateTime -lpUserTime.dwLowDateTime ));wprintf(szw);getchar();return 0;
}


 

0 0
原创粉丝点击