[Programming Pearl] Chapter 6 Program Performance Analysis

Source: Internet
Author: User

I. Overview

To improve the performance of the software, you need to start from the following aspects:

1. algorithms and data structures

2. Algorithm Optimization

3. Data restructuring

4. Optimization of code unrelated to the system (float replaces double ).

5. System-related optimization: accelerates frequently-used functions. For example, key code uses Assembly instead of advanced languages.

6. Use a floating point accelerator on the hardware.


Ii. Exercise

2) factorization

Example: a positive integer n greater than 1 can be divided into n = x1 * X2 *... * XM.
For example, when n = 12, there are 8 different decomposition types:
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;

Programming task:
For a given positive integer N, It is programmed to calculate the total number of different decomposition formulas of N.
Input
There are multiple groups of input data, one row of each group of data, is a positive integer N (1 <= n <= 2000000000 ).
Output
Enter the number of Factorization formula calculated. After each group of data, a carriage return is output.
Sample Input
12
Sample output
8


The program that can output each equation in the test (12 = 12 requires manual output, but the program does not)

# 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; // record if (a % I = 0) {If (last! = 0) // If you enter this method, it indicates that the factor to be decomposed {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 ); // recursive call }}return 0;} bool decompose (int A) // test whether it can be decomposed {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 ;}

Programs that return only the number of records (recursively called)

# Include <stdio. h> int count; int num; void derive (int A) // use recursive method to obtain the expected value {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) is efficiency always behind correctness?

There are 10 known errors in a large program today and 10 new errors in the next month. If you want to change the current 10 errors and speed up the program by 10 times, Which one will you choose?


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.