08/01/24 21:08:11
メモリアクセスに時間かかるようなのでパラメータ変更しました これだと微妙に論理演算のほうが速いかも
#include <iostream>
#include <time.h>
using namespace std;
#define N 10000
#define rd() (rand()&255)
#define rnd() rd()+(rd()<<8)+(rd()<<16)+(rd()<<24)
main(){
unsigned int *a=new unsigned int [N];
unsigned int *b=new unsigned int [N];
int n,cl,clsum[3],s=0;
for(n=0;n<3;n++)clsum[n]=0;
for(n=0;n<N;n++){ a[n]=rnd(); b[n]=rnd();}
for(int k=0;k<30000;k++){
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<<"if文の速度 "<<clsum[0]<<endl;
cout<<"if無しの速度1 "<<clsum[1]<<endl;
cout<<"if無しの速度2 "<<clsum[2]<<endl;
cout<<(s&1);
}