【C\C++笔记】register寄存器关键字
使用寄存器变量提高运行速度
1未使用寄存器组
#include<stdio.h>
int main(){
unsigned long a=0;
for(int i=0;i<100000;i++){
for(int j=0;j<i;j++){
a+=i;
a-=i;
}
a+=i;
}
printf("%d",a);
return 0;
}
结果14s
704982704 -------------------------------- Process exited after 14.64 seconds with return value 0
2使用寄存器组
#include<stdio.h>
int main(){
register unsigned long a=0;
for(register int i=0;i<100000;i++){
for(register int j=0;j<i;j++){
a+=i;
a-=i;
}
a+=i;
}
printf("%d",a);
return 0;
}
结果3s
704982704 -------------------------------- Process exited after 2.954 seconds with return value 0

浙公网安备 33010602011771号