The amazing efficiency of pointer in C Programming Language

本文通过三种不同方式展示了如何使用C语言中的指针来高效地复制数组,并对比了它们在转换为汇编语言后的执行效率。文章指出,利用指针直接操作内存可以显著提高程序运行速度。

      Today I read a book names C Programming Language and Pointer,In some section I found an amazing efficiency programme by using pointer,the example's topic is copy an array.you will see the effect after these codes compiled to assembly language. 
      Here is the example:
      Suppose we already know to array A and B,and now we want to copy A to B,assume A and B have a same size we just define it is SIZE,the following I will show some ways of array copy.
      The condition is:
      #define SIZE 50
      int A[SIZE]
      int B[SIZE]
NO 1
void copy1()
{
     for(i = 0; i < SIZE; i++)
             A[i] = B[i];
}
this way is very clear,it is very easy to read,but with low efficiency.

NO 2
void copy2()
{
       int *p1, *p2
       for(int i = 0, p1 = A, p2 = B; i < SIZE; i++)
            *p2++ = *p1++;
}
this way is clear ,too.and the efficiency will improves while comared to the preceding way.

NO 3|
void copy3()
{
       register int *p1, *p2;
       for(p1 = A, p2 = B; p1 <= &A[Size - 1];)
             *p2++ = *p1++;
}
this is the most efficiency way for two array copy.but it is not so clear while compared to the preceding two ways.

       Why C programming language is so efficiency? the most reason I think is it can manipulate the memory and harewares directly.if you wite a good C programme the excute speed is more or less the same as Assembly Language.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值