(简单问题)ISDIGIT算法问题:两次比较与取地址表查找比较谁更快

GKatHere 2017-01-10 04:05:42
在《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') ) // 124800
for (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') )//1404000
for (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; //4056026 MSVCR100.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;}
...全文
892 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

2,408

社区成员

发帖
与我相关
我的任务
社区描述
高性能计算
社区管理员
  • 高性能计算社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧