Sunwayking

导航

将数值(整型或其他类型均可)按二进制位打印出来的方法

#include "stdafx.h"
#include 
<iostream>
#include 
<math.h>
#include 
<Windows.h>
using namespace std;
#define N (int)(sizeof(int)*8)

#define SLOWMETHORD

unsigned 
int GetCPUTickCount(){ 
    _asm 
    { 
        rdtsc; 
    } 



int _tmain(int argc, _TCHAR* argv[])
{
#ifdef SLOWMETHORD
    
//方法一:利用数学库函数,速度慢
    cout<<"(Slow Methord)Enter a Number:"<<endl;
    
int x;
    
int a[N]={0};
    
int timer=0;
    cin
>>x;
    cout
<<"Result: "<<endl<<endl;
    timer-
=GetCPUTickCount();
    
for(int i=0;i<N;i++)
    {
        a[i]
=(x&(int)pow(2.0,N-i-1))>>(N-i-1);
        cout
<<a[i];
        
if ((i+1)%4==0)
        {
            cout
<<"   ";
        }
        
else
        {
            cout
<<" ";
        }
    }
    cout
<<endl;
    timer
+=GetCPUTickCount();
    cout
<<"Time Cost: "<<timer<<endl;
    system(
"pause");
#else
    
//方法二:纯位运算,速度快
    cout<<"(Fast Methord)Enter a Number:"<<endl;
    
int x;
    
int a[N]={0};
    
int timer=0;
    cin
>>x;
    cout
<<"Result: "<<endl;
    timer-
=GetCPUTickCount();
    
for(int i=0;i<N;i++)
    {
        
int r=(1<<(N-1-i));
        a[i]
=(x&r)>>(N-i-1);
        cout
<<a[i];
        
if ((i+1)%4==0)
        {
            cout
<<"   ";
        }
        
else
        {
            cout
<<" ";
        }
    }
    cout
<<endl;
    timer
+=GetCPUTickCount();
    cout
<<"Time Cost: "<<timer<<endl;
    system(
"pause");
#endif
    
return 0;
}
 
上面提供了两种方法,其实道理都是一样的.
下面给出两种方法的耗时对比(同一数值,连续运行5次求平均数):
Slow:平均16984492.8
17179440
16361088
16946448
17293256
17142232

Fast:平均16363368
16490104
17221552
17098680
13827672
17178832
 
下面给出第三种方法,效率更高.
cout<<"(Fast Methord)Enter a Number:"<<endl;
int x;
int a[N]={0};
int timer=0;
cin
>>x;
cout
<<"Result: "<<endl;
timer-
=GetCPUTickCount();
int off;
int result;
for(int i=N-1;i>=0;i--)
{
result
=0;
__asm
{
MOV eax,i
BT x, eax
JNC labelend
MOV result,
1
labelend:
}
cout
<<result;
cout
<<"";
}
cout
<<endl;
timer
+=GetCPUTickCount();
cout
<<"Time Cost: "<<timer<<endl;
system(
"pause");

 利用位域达到最佳性能:

struct stINT32_Bits
{
    unsigned bit00:1;
    unsigned bit01:1;
    unsigned bit02:1;
    unsigned bit03:1;
    unsigned bit04:1;
    unsigned bit05:1;
    unsigned bit06:1;
    unsigned bit07:1;
    unsigned bit08:1;
    unsigned bit09:1;
    unsigned bit10:1;
    unsigned bit11:1;
    unsigned bit12:1;
    unsigned bit13:1;
    unsigned bit14:1;
    unsigned bit15:1;
    unsigned bit16:1;
    unsigned bit17:1;
    unsigned bit18:1;
    unsigned bit19:1;
    unsigned bit20:1;
    unsigned bit21:1;
    unsigned bit22:1;
    unsigned bit23:1;
    unsigned bit24:1;
    unsigned bit25:1;
    unsigned bit26:1;
    unsigned bit27:1;
    unsigned bit28:1;
    unsigned bit29:1;
    unsigned bit30:1;
    unsigned bit31:1;    
};
union stINT32
{
    int intVal;
    stINT32_Bits bits;
};

    cout<<"(Fast Methord)Enter a Number:"<<endl;
    int x;
    int a[N]={0};
    int timer=0;
    cin>>x;
    cout<<"Result: "<<endl;
    timer-=GetCPUTickCount();
    stINT32 val;
    val.intVal=x;
    cout<<val.bits.bit00<<" "
        <<val.bits.bit01<<" "
        <<val.bits.bit02<<" "
        <<val.bits.bit03<<" "
        <<val.bits.bit04<<" "
        <<val.bits.bit05<<" "
        <<val.bits.bit06<<" "
        <<val.bits.bit07<<" "
        <<val.bits.bit08<<" "
        <<val.bits.bit09<<" "
        <<val.bits.bit10<<" "
        <<val.bits.bit11<<" "
        <<val.bits.bit12<<" "
        <<val.bits.bit13<<" "
        <<val.bits.bit14<<" "
        <<val.bits.bit15<<" "
        <<val.bits.bit16<<" "
        <<val.bits.bit17<<" "
        <<val.bits.bit18<<" "
        <<val.bits.bit19<<" "
        <<val.bits.bit20<<" "
        <<val.bits.bit21<<" "
        <<val.bits.bit22<<" "
        <<val.bits.bit23<<" "
        <<val.bits.bit24<<" "
        <<val.bits.bit25<<" "
        <<val.bits.bit26<<" "
        <<val.bits.bit27<<" "
        <<val.bits.bit28<<" "
        <<val.bits.bit29<<" "
        <<val.bits.bit30<<" "
        <<val.bits.bit31<<endl;
    timer+=GetCPUTickCount();
    cout<<"Time Cost: "<<timer<<endl;
    system("pause");
    return 0;

 

posted on 2009-11-26 18:16  Sunwayking  阅读(546)  评论(0编辑  收藏  举报