温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

让C程序更高效的10种方法分别是哪些

发布时间:2021-10-29 17:21:08 来源:亿速云 阅读:100 作者:柒染 栏目:编程语言

让C程序更高效的10种方法分别是哪些,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

代码之美,不仅在于为一个给定问题找到解决方案,而且还在代码的简单性、有效性、紧凑性和效率(内存)。代码设计比实际执行更难 。因此,每一个程序员当用C语言编程时,都应该记着这些东西。向你介绍规范你的C代码的10种方法。

0. 避免不必要的函数调用

考虑下面的2个函数:

  1. void str_print( char *str ) 

  2.   

  3.   

  4.     int i; 

  5.   

  6.     for ( i = 0; i < strlen ( str ); i++ ) { 

  7.   

  8.         printf("%c",str[ i ] ); 

  9.   

  10.     } 

  11.   

  12. void str_print1 ( char *str ) 

  13.   

  14.   

  15.     int len; 

  16.   

  17.     len = strlen ( str ); 

  18.   

  19.     for ( i = 0; i < len; i++ ) { 

  20.   

  21.         printf("%c",str[ i ] ); 

  22.   

  23.     } 

  24.   

  25. }

<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif; font-size: 13px; line-height: 19px; white-space: normal;">请注意 这两个函数的功能相似。然而,***个函数调用strlen()函数多次,而第二个函数只调用函数strlen()一次。因此***个函数性能明显比第二个好。</span><span style="color: red;"><strong>(更新:原作者应该是笔误,把***个函数写成优于第二个,否则自相矛盾。)</strong></span>

1、避免不必要的内存引用

这次我们再用2个例子来对比解释:

int multiply ( int *num1 , int *num2 )   {       *num1 = *num2;       *num1 += *num2;       return *num1;   } int multiply1 ( int *num1 , int *num2 )   {       *num1 = 2 * *num2;       return *num1;   }
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif; font-size: 13px; line-height: 19px; white-space: normal;">同样,这两个函数具有类似的功能。所不同的是在***个函数( 1 for reading *num1 , 2 for reading *num2 and 2 for writing to *num1)有5个内存的引用,而在第二个函数是只有2个内存引用(one for reading *num2 and one for writing to *num1)。现在你认为哪一个好些?</span>

2、节约内存(内存对齐和填充的概念)

struct {       char c;       int i;       short s;   }str_1; struct {       char c;       short s;       int i;   }str_2;

假设一个字符需要1个字节,short占用2个字节和int需要4字节的内存。起初,我们会认为上面定义的结构是相同的,因此占据相同数量的内存。然而,而str_1占用12个字节,第二个结构只需要8个字节?这怎么可能呢?

请注意,在***个结构,3个不同的4个字节被分配到三种数据类型,而在第二个结构的前4个自己char和short可以被采用,int可以采纳在第二个的4个字节边界(一共8个字节)。

3、使用无符号整数,而不是整数的,如果你知道的值将永远是否定的。

有些处理器可以处理无符号的整数比有符号整数的运算速度要快。(这也是很好的实践,帮助self-documenting代码)。

4、在一个逻辑条件语句中常数项永远在左侧。

int x = 4;   if ( x = 1 ) {       x = x + 2;       printf("%d",x);          // Output is 3   } int x = 4;   if ( 1 = x ) {       x = x + 2;       printf("%d",x);   // Compilation error   }

使用“=”赋值运算符,替代“==”相等运算符,这是个常见的输入错误。  常数项放在左侧,将产生一个编译时错误,让你轻松捕获你的错误。注:“=”是赋值运算符。 b = 1会设置变量b等于值1。  “==”相等运算符。如果左侧等于右侧,返回true,否则返回false。

5、在可能的情况下使用typedef替代macro。当然有时候你无法避免macro,但是typedef更好。

typedef int* INT_PTR;   INT_PTR a , b;   # define INT_PTR int*;   INT_PTR a , b;

在这个宏定义中,a是一个指向整数的指针,而b是只有一个整数声明。使用typedef a和b都是 整数的指针。

6、确保声明和定义是静态的,除非您希望从不同的文件中调用该函数。

在同一文件函数对其他函数可见,才称之为静态函数。它限制其他访问内部函数,如果我们希望从外界隐藏该函数。现在我们并不需要为内部函数创建头文件,其他看不到该函数。

静态声明一个函数的优点包括:

  1. 两个或两个以上具有相同名称的静态函数,可用于在不同的文件。

  2. 编译消耗减少,因为没有外部符号处理。

让我们做更好的理解,下面的例子:

  1. /*first_file.c*/ 

  2.   

  3. static int foo ( int a ) 

  4.   

  5.   

  6. /*Whatever you want to in the function*/ 

  7.   

  8.   

  9. /*second_file.c*/ 

  10.   

  11. int foo ( int ) 

  12.   

  13. int main() 

  14.   

  15.   

  16.     foo();   // This is not a valid function call as the function foo can only be called by any other function within first_file.c where it is defined. 

  17.   

  18.     return 0; 

  19.   

  20. }

  1. <strong style="font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif; font-size: 13px; line-height: 19px; white-space: normal;"> </strong>

<strong style="font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif; font-size: 13px; line-height: 19px; white-space: normal;">7、使用Memoization,以避免递归重复计算</strong>

考虑Fibonacci(斐波那契)问题;

Fibonacci问题是可以通过简单的递归方法来解决:

  1. int fib ( n ) 

  2.   

  3.   

  4.     if ( n == 0 || n == 1 ) { 

  5.   

  6.         return 1; 

  7.   

  8.     } 

  9.   

  10.     else { 

  11.   

  12.         return fib( n - 2 ) + fib ( n - 1 ); 

  13.   

  14.     } 

  15.   

  16. }

<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif; font-size: 13px; line-height: 19px; white-space: normal;">注:在这里,我们考虑Fibonacci 系列从1开始,因此,该系列看起来:1,1,2,3,5,8,...</span>
<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif; font-size: 13px; line-height: 19px; white-space: normal;"><a href="https://cache.yisu.com/upload/information/20210521/332/450980"><img class="aligncenter" title="fibonacci-recursion-tree" src="https://cache.yisu.com/upload/information/20210521/332/450982" alt="" width="300" height="174" /></a> </span>

注意:从递归树,我们计算fib(3)函数2次,fib(2)函数3次。这是相同函数的重复计算。如果n非常大,fib

这个简单的技术叫做Memoization,可以被用在递归,加强计算速度。

fibonacci 函数Memoization的代码,应该是下面的这个样子:

  1. int calc_fib ( int n ) 

  2.   

  3.   

  4.     int val[ n ] , i; 

  5.   

  6.     for ( i = 0; i <=n; i++ ) { 

  7.   

  8.         val[ i ] = -1;      // Value of the first n + 1 terms of the fibonacci terms set to -1 

  9.   

  10.     } 

  11.   

  12.     val[ 0 ] = 1;               // Value of fib ( 0 ) is set to 1 

  13.   

  14.     val[ 1 ] = 1;           // Value of fib ( 1 ) is set to 1 

  15.   

  16.     return fib( n , val ); 

  17.   

  18.   

  19. int fib( int n , int* value ) 

  20.   

  21.   

  22.     if ( value[ n ] != -1 ) { 

  23.   

  24.         return value[ n ];              // Using memoization 

  25.   

  26.     } 

  27.   

  28.     else { 

  29.   

  30.         value[ n ] = fib( n - 2 , value ) + fib ( n - 1 , value );          // Computing the fibonacci term 

  31.   

  32.     } 

  33.   

  34.     return value[ n ];                // Returning the value 

  35.   

  36. }

<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif; font-size: 13px; line-height: 19px; white-space: normal;">这里calc_fib( n )函数被main()调用。</span>

8、避免悬空指针和野指针

一个指针的指向对象已被删除,那么就成了悬空指针。野指针是那些未初始化的指针,需要注意的是野指针不指向任何特定的内存位置。

  1. void dangling_example() 

  2.   

  3.   

  4.     int *dp = malloc ( sizeof ( int )); 

  5.   

  6.     /*........*/ 

  7.   

  8.     free( dp );             // dp is now a dangling pointer 

  9.   

  10.     dp = NULL;      // dp is no longer a dangling pointer 

  11.   

  12.   

  13. void wild_example() 

  14.   

  15.   

  16.     int *ptr;       // Uninitialized pointer 

  17.   

  18.     printf("%u"\n",ptr ); 

  19.   

  20.     printf("%d",*ptr ); 

  21.   

  22. }

<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif; font-size: 13px; line-height: 19px; white-space: normal;">当遭遇这些指针,程序通常是”怪异“的表现。</span>

9、 永远记住释放你分配给程序的任何内存。上面的例子就是如果释放dp指针(我们使用malloc()函数调用)。

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI