07/11/22 15:05:40
計ってみたよ 配列に何度も入れた方がわずかに速いようだ
#include <iostream>
#include <time.h>
#define N 900000
using namespace std;
main(){
unsigned int n,k,c,sum;
unsigned int *x,*y,*z,*v,*w;
x=new unsigned int [N]; y=new unsigned int [N];
z=new unsigned int [N]; v=new unsigned int [N];
w=new unsigned int [N];
for(n=0;n<N;n++){x[n]=rand()%10;y[n]=rand()%N;z[n]=rand()%N;v[n]=rand()%N;w[n]=rand()%N;}
sum=0; c=clock();
for(n=0;n<N;n++){k=w[n];k=v[k];k=z[k];k=y[k];sum+=x[k];}
c=clock()-c; cout<<c<<" clock "<<sum<<endl;
sum=0; c=clock();
for(n=0;n<N;n++)sum+=x[y[z[v[w[n]]]]];
c=clock()-c; cout<<c<<" clock "<<sum<<endl;
}