你的循环展开是真的吗?

正确写法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<bits/stdc++.h>
using namespace std;
int a[150005],n=120000;
int solve(){
long long sum1=rand()%n+1,sum2=rand()%n+1,sum3=rand()%n+1,sum4=rand()%n+1,sum5=rand()%n+1,sum6=rand()%n+1,sum7=rand()%n+1,sum8=rand()%n+1;
for(int i=0;i+8<=n;i+=8){
sum1+=a[i+1];sum2+=a[i+2];sum3+=a[i+3];sum4+=a[i+4];
sum5+=a[i+5];sum6+=a[i+6];sum7+=a[i+7];sum8+=a[i+8];
}
switch(n&7){
case 7:sum7+=a[n-6];
case 6:sum6+=a[n-5];
case 5:sum5+=a[n-4];
case 4:sum4+=a[n-3];
case 3:sum3+=a[n-2];
case 2:sum2+=a[n-1];
case 1:sum1+=a[n];
}
return sum1+sum2+sum3+sum4+sum5+sum6+sum7+sum8;
}
int main(){
srand(102624);
for(int i=1;i<=n;++i)a[i]=i;
long long ans=0;
for(int i=1;i<=100000;++i)ans^=solve();
cerr<<ans<<' '<<1.0*clock()/CLOCKS_PER_SEC<<'s'<<'\n';
return 0;
}

测试结果:

正常 5.822s

开O2:1.647s

循环用了++i

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<bits/stdc++.h>
using namespace std;
int a[150005],n=120000;
int solve(){
long long sum1=rand()%n+1,sum2=rand()%n+1,sum3=rand()%n+1,sum4=rand()%n+1,sum5=rand()%n+1,sum6=rand()%n+1,sum7=rand()%n+1,sum8=rand()%n+1;
for(int i=0;i+8<=n;){
sum1+=a[++i];sum2+=a[++i];sum3+=a[++i];sum4+=a[++i];
sum5+=a[++i];sum6+=a[++i];sum7+=a[++i];sum8+=a[++i];
}
switch(n&7){
case 7:sum7+=a[n-6];
case 6:sum6+=a[n-5];
case 5:sum5+=a[n-4];
case 4:sum4+=a[n-3];
case 3:sum3+=a[n-2];
case 2:sum2+=a[n-1];
case 1:sum1+=a[n];
}
return sum1+sum2+sum3+sum4+sum5+sum6+sum7+sum8;
}
int main(){
srand(102624);
for(int i=1;i<=n;++i)a[i]=i;
long long ans=0;
for(int i=1;i<=100000;++i)ans^=solve();
cerr<<ans<<' '<<1.0*clock()/CLOCKS_PER_SEC<<'s'<<'\n';
return 0;
}

测试结果:

正常 7.343s

开O2:1.647s

求和变量用了同一个:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<bits/stdc++.h>
using namespace std;
int a[150005],n=120000;
int solve(){
long long sum=rand()%n+1+rand()%n+1+rand()%n+1+rand()%n+1+rand()%n+1+rand()%n+1+rand()%n+1+rand()%n+1;
for(int i=0;i+8<=n;i+=8){
sum+=a[i+1];sum+=a[i+2];sum+=a[i+3];sum+=a[i+4];
sum+=a[i+5];sum+=a[i+6];sum+=a[i+7];sum+=a[i+8];
}
switch(n&7){
case 7:sum+=a[n-6];
case 6:sum+=a[n-5];
case 5:sum+=a[n-4];
case 4:sum+=a[n-3];
case 3:sum+=a[n-2];
case 2:sum+=a[n-1];
case 1:sum+=a[n];
}
return sum;
}
int main(){
srand(102624);
for(int i=1;i<=n;++i)a[i]=i;
long long ans=0;
for(int i=1;i<=100000;++i)ans^=solve();
cerr<<ans<<' '<<1.0*clock()/CLOCKS_PER_SEC<<'s'<<'\n';
return 0;
}

测试结果:

正常 24.492s

开O2:2.961s