程序优化的一些方法

1.类型限定词restrict:对于某指针是访问一个数据对象的唯一且初始的方式,则可以用restrict来修饰
int ar[10];
int* restrict restar = (int*)malloc(10*sizeof(int));
int* par = ar;
for(n = 0 ; n < 10 ;n++)
{
    par[n] += 5;
    restar[n] += 5;
    ar[n] *= 2;
    par[n] +=3;
    restar[n] += 3;
}
11
1
int ar[10];
2
int* restrict restar = (int*)malloc(10*sizeof(int));
3
int* par = ar;
4
for(n = 0 ; n < 10 ;n++)
5
{
6
    par[n] += 5;
7
    restar[n] += 5;
8
    ar[n] *= 2;
9
    par[n] +=3;
10
    restar[n] += 3;
11
}
通过restrict修饰后,编译器就知道了restar是访问那块内存的唯一且初始的方式,因此就会把for循环里的代码优化为 restar[n] += 8;C Prime Plus P348

2.除法乘法运算用移位运算来代替会提高程序的运行速度。

3.很多机器上下标运算都要比指针运算慢。

4.函数调用需要花费比较长的程序执行时间,因此如果是简单的函数可以考虑使用宏定义。




posted @ 2018-07-17 21:28  LyndonMario  阅读(512)  评论(0编辑  收藏  举报