【编程珠玑】第六章 程序性能分析
2012-05-14 16:01
155 查看
一,概述
如果要提高软件的性能,需要从下面几个方面入手:
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需要手动输出,程序没给出)
仅仅返回个数的程序(递归调用)
6)效率永远排在正确性后面?
一个大型程序,今天有10个已知的错误,下个月又会有10个新的错误。如果让你在更改当前10个错误和使程序提速10倍,你会选择哪一个?
如果要提高软件的性能,需要从下面几个方面入手:
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需要手动输出,程序没给出)
#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> 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倍,你会选择哪一个?
相关文章推荐
- 【编程珠玑】第六章 程序性能分析
- 【编程珠玑】读书笔记 第六章 程序性能分析
- 6、编程珠玑笔记六程序性能分析
- (5~7)编程小事、程序性能分析及粗略估算
- 写网络程序要做好充分的性能分析工作--编程心得
- 基于visual c++之windows核心编程代码分析(44)监测任意程序函数起始地址
- 如何使用strace+pstack利器分析程序性能
- VS2010自带的性能分析工具分析.NET程序的性能
- 用 Hadoop 进行分布式并行编程, 第 2 部分 程序实例与分析
- 程序性能分析工具
- 【编程珠玑】第三章 数据决定程序结构
- java程序性能分析用到的文件thread dump和heap dump入门
- C/C++ 程序的性能分析工具 google-perftools
- Python程序的性能分析指南(转)
- Linux C程序性能分析
- miniprofiler-----Stack Overflow团队设计的一款对ASP.NET MVC的性能分析的小程序
- 编程之美 2.20程序理解和时间分析
- NET程序内存分析工具CLRProfiler的使用(性能测试)
- 【编程珠玑】第四章:编写正确的程序
- VS程序性能分析器 -- 使用说明