程序性能分析

程序性能分析

一,概述

       如果要提高软件的性能,需要从下面几个方面入手:

        1、算法与数据结构

        2、算法调优

        3、数据结构重组

        4、与系统无关的代码的调优(float取代double)。

        5、与系统相关的调优,把经常使用的函数进行加速,比如关键代码使用汇编代替高级语言

        6、硬件上使用浮点加速器。


二,习题

       2)因子分解

            例题:大于1的正整数n可以分解为:n = x1*x2*...*xm。
                例如:当n = 12时,共有8中不同的分解式:
12 = 12;
12 = 6 * 2;
12 = 4 * 3;
12 = 3 * 4;
12 = 3 * 2 * 2;
12 = 2 * 6;
12 = 2 * 3 * 2;
12 = 2 * 2 * 3;

编程任务:
对于给定的正整数 n, 编程计算n共有多少种不同的分解式。
Input
有多组输入数据,每组数据的一行,为一个正整数n( 1 < = n < = 2000000000 )。
Output
输入计算出的分解式的数目。每组数据后输出一个回车。
Sample Input
12
Sample Output
8


测试可以输出每个等式的程序(12 = 12需要手动输出,程序没给出)

[html]
#include <stdio.h>  
  1. int fun(int a,int* aaa,int last=0);  
  2. bool Decompose(int a);  
  3. int fun(int a,int* aaa,int last)  
  4. {  
  5.     for(int i=a-1;i>1;i--)  
  6.     {  
  7.         int num=last;//记录  
  8.           
  9.         if(a % i == 0)  
  10.         {  
  11.             if(last != 0) //如果进入这个方法,说明因子待分解  
  12.             {  
  13.                 int temp=a;  
  14.                 for(int j=0;j<last;j++)  
  15.                     temp*=aaa[j];  
  16.                 printf("%d=",temp);  
  17.                 for(int j=0;j<last;j++)  
  18.                     printf("%d*",aaa[j]);  
  19.             }  
  20.             else  
  21.                 printf("%d=",a);  
  22.             aaa[num++]=i;  
  23.             printf("%d*%d\n",i,a/i);  
  24.             if(Decompose(a/i))  
  25.             {  
  26.                 fun(a/i,aaa,num); //递归调用  
  27.             }  
  28.         }  
  29.     }  
  30.     return 0;  
  31. }  
  32.   
  33. bool Decompose(int a)//测试还能不能分解  
  34. {  
  35.     for(int i=a-1;i>1;i--)  
  36.     {  
  37.         if(a % i == 0)  
  38.         {  
  39.             return true;  
  40.         }  
  41.     }  
  42.     return false;  
  43. }  
  44. int main()  
  45. {  
  46.     int num=12;  
  47.     int numlist[12];  
  48.     fun(num,numlist);  
  49.     return 0;  
  50. }  
#include <stdio.h> int fun(int a,int* aaa,int last=0); bool Decompose(int a); int fun(int a,int* aaa,int last) { for(int i=a-1;i>1;i--) { int num=last;//记录 if(a % i == 0) { if(last != 0) //如果进入这个方法,说明因子待分解 { int temp=a; for(int j=0;j<last;j++) temp*=aaa[j]; printf("%d=",temp); for(int j=0;j<last;j++) printf("%d*",aaa[j]); } else printf("%d=",a); aaa[num++]=i; printf("%d*%d\n",i,a/i); if(Decompose(a/i)) { fun(a/i,aaa,num); //递归调用 } } } return 0; } bool Decompose(int a)//测试还能不能分解 { for(int i=a-1;i>1;i--) { if(a % i == 0) { return true; } } return false; } int main() { int num=12; int numlist[12]; fun(num,numlist); return 0; }

仅仅返回个数的程序(递归调用)

#include<stdio.h>   
  1. int count;   
  2. int num;   
  3. void derive(int a) //采用递归方法得到所要的数值   
  4. {   
  5.  int i;   
  6.  for(i=a-1;i>1;i--)   
  7.   if((a % i)==0)   
  8.   {   
  9.    count++;   
  10.    derive(a/i);   
  11.   }   
  12. }   
  13. int main()   
  14. {   
  15.  while(1)   
  16.  {   
  17.   printf("\nplease input a number:");   
  18.   scanf("%d",&num);   
  19.   if(num==0) break;   
  20.   count=1;   
  21.   derive(num);   
  22.   printf("\nthere are %d comforted",count);   
  23.  }   
  24.  return 0;  
  25. }   
#include<stdio.h> int count; int num; void derive(int a) //采用递归方法得到所要的数值 { int i; for(i=a-1;i>1;i--) if((a % i)==0) { count++; derive(a/i); } } int main() { while(1) { printf("\nplease input a number:"); scanf("%d",&num); if(num==0) break; count=1; derive(num); printf("\nthere are %d comforted",count); } return 0; }


       6)效率永远排在正确性后面?

       一个大型程序,今天有10个已知的错误,下个月又会有10个新的错误。如果让你在更改当前10个错误和使程序提速10倍,你会选择哪一个?

你可能感兴趣的:(程序性能分析)