- 注册时间
- 2008-2-6
- 最后登录
- 1970-1-1
- 威望
- 星
- 金币
- 枚
- 贡献
- 分
- 经验
- 点
- 鲜花
- 朵
- 魅力
- 点
- 上传
- 次
- 下载
- 次
- 积分
- 51573
- 在线时间
- 小时
|
发表于 2008-3-10 10:07:43
|
显示全部楼层
附详细代码
并请终止任何当前占CPU程序
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
DWORD bits[32];
void find(DWORD a, DWORD * b, DWORD *c)
{
__asm
{
mov eax, dword ptr [a]
xor esi, esi
loop1:
bsr edi, eax
je exit1
xor eax, dword ptr [bits + 4 * edi]
mov dword ptr [b + 4*esi], edi
inc esi
jmp loop1
exit1:
mov dword ptr [c], esi
}
}
DWORD find_by_GxQ(DWORD code, DWORD index[33])
{
__asm
{
mov ecx, code;
xor eax, eax;
shr ecx, 1;
mov dword ptr[index + 4*eax], 0;
adc eax, 0;
shr ecx, 1;
mov dword ptr[index + 4*eax], 1;
adc eax, 0;
shr ecx, 1;
mov dword ptr[index + 4*eax], 2;
adc eax, 0;
shr ecx, 1;
mov dword ptr[index + 4*eax], 3;
adc eax, 0;
shr ecx, 1;
mov dword ptr[index + 4*eax], 4;
adc eax, 0;
shr ecx, 1;
mov dword ptr[index + 4*eax], 5;
adc eax, 0;
shr ecx, 1;
mov dword ptr[index + 4*eax], 6;
adc eax, 0;
shr ecx, 1;
mov dword ptr[index + 4*eax], 7;
adc eax, 0;
shr ecx, 1;
mov dword ptr[index + 4*eax], 8;
adc eax, 0;
shr ecx, 1;
mov dword ptr[index + 4*eax], 9;
adc eax, 0;
shr ecx, 1;
mov dword ptr[index + 4*eax], 10;
adc eax, 0;
shr ecx, 1;
mov dword ptr[index + 4*eax], 11;
adc eax, 0;
shr ecx, 1;
mov dword ptr[index + 4*eax], 12;
adc eax, 0;
shr ecx, 1;
mov dword ptr[index + 4*eax], 13;
adc eax, 0;
shr ecx, 1;
mov dword ptr[index + 4*eax], 14;
adc eax, 0;
shr ecx, 1;
mov dword ptr[index + 4*eax], 15;
adc eax, 0;
shr ecx, 1;
mov dword ptr[index + 4*eax], 16;
adc eax, 0;
shr ecx, 1;
mov dword ptr[index + 4*eax], 17;
adc eax, 0;
shr ecx, 1;
mov dword ptr[index + 4*eax], 18;
adc eax, 0;
shr ecx, 1;
mov dword ptr[index + 4*eax], 19;
adc eax, 0;
shr ecx, 1;
mov dword ptr[index + 4*eax], 20;
adc eax, 0;
shr ecx, 1;
mov dword ptr[index + 4*eax], 21;
adc eax, 0;
shr ecx, 1;
mov dword ptr[index + 4*eax], 22;
adc eax, 0;
shr ecx, 1;
mov dword ptr[index + 4*eax], 23;
adc eax, 0;
shr ecx, 1;
mov dword ptr[index + 4*eax], 24;
adc eax, 0;
shr ecx, 1;
mov dword ptr[index + 4*eax], 25;
adc eax, 0;
shr ecx, 1;
mov dword ptr[index + 4*eax], 26;
adc eax, 0;
shr ecx, 1;
mov dword ptr[index + 4*eax], 27;
adc eax, 0;
shr ecx, 1;
mov dword ptr[index + 4*eax], 28;
adc eax, 0;
shr ecx, 1;
mov dword ptr[index + 4*eax], 29;
adc eax, 0;
shr ecx, 1;
mov dword ptr[index + 4*eax], 30;
adc eax, 0;
shr ecx, 1;
mov dword ptr[index + 4*eax], 31;
adc eax, 0;
// 休止符
mov dword ptr[index + 4*eax], -1;
}
}
int main(int argc, char * argv[])
{
::SetThreadPriority(::GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
LARGE_INTEGER lTimestart, lTimeEnd, liFreq;
DWORD c, bitMask = 1, index[33];
for (DWORD i = 0; i < 31; i ++)
{
bits[i] = bitMask;
bitMask <<= 1;
}
QueryPerformanceCounter(&lTimestart);
for(DWORD i = 0; i < 1000000; i++){
find(i, &index[0], &c);// 调用bit扫描函数
}
QueryPerformanceCounter(&lTimeEnd);
QueryPerformanceFrequency(&liFreq);
double dbMs = ((double) (lTimeEnd.QuadPart - lTimestart.QuadPart) * 1000) / (double)liFreq.QuadPart;
printf("yaos: %.3f\n", dbMs);
QueryPerformanceCounter(&lTimestart);
for(DWORD i = 0; i < 1000000; i++){
find_by_GxQ(i, index);// 调用bit扫描函数
}
QueryPerformanceCounter(&lTimeEnd);
QueryPerformanceFrequency(&liFreq);
dbMs = ((double) (lTimeEnd.QuadPart - lTimestart.QuadPart) * 1000) / (double)liFreq.QuadPart;
printf("GXQ: %.3f\n", dbMs);
system("Pause");
return 0;
} |
|