08/01/24 21:58:55
安定して正確な速度がはかれるっぽいコードです・・・スタック領域に少量確保しました わずかに論理式のほうが速いかもしれないです
#include <iostream>
#include <time.h>
using namespace std;
#define kukikaesi 300000
main(){
#define N 1000
unsigned int a[N],b[N];
int n,cl,clsum[4],s=0,t=0;
for(n=0;n<4;n++)clsum[n]=0;
for(n=0;n<N;n++){
#define rd() (rand()&255)
#define rnd() rd()+(rd()<<8)+(rd()<<16)+(rd()<<24)
a[n]=rnd(); b[n]=rnd();}
for(int k=0;k<kukikaesi;k++){
cl=clock();for(n=0;n<N;n++)t|=a[n]&b[n]&1;clsum[3]+=clock()-cl;
cl=clock();for(n=0;n<N;n++)s+=(a[n]>UINT_MAX-b[n]);clsum[0]+=clock()-cl;
cl=clock();for(n=0;n<N;n++)s+=((a[n]>>1)+(b[n]>>1)+(a[n]&b[n]&1))>>31;clsum[1]+=clock()-cl;
cl=clock();for(n=0;n<N;n++){static unsigned int x=a[n],y=b[n];s+=((x>>1)+(y>>1)+(x&y&1))>>31;}clsum[2]+=clock()-cl;
}
cout<<"比較演算の速度 "<<clsum[0]<<endl;
cout<<"論理演算の速度1 "<<clsum[1]<<endl;
cout<<"論理演算の速度2 "<<clsum[2]<<endl;
cout<<"単純な論理演算の速度 "<<clsum[3]<<endl;
cout<<(s&t&1);
}