[Programming Pearl (continued)] Chapter 1 performance monitoring tools

Source: Internet
Author: User

1. Calculate the prime number

First Program: If the integer n cannot be divisible from 2 to n-1, It is a prime number. Note that n = 2 must be a prime number.

#include <iostream>using namespace std;int prime(int n){           int i;        for (i = 2; i < n; i++)                if(n%i == 0)                        return 0;        return 1;}int main(){    int i,n;    n=1000;    for(i = 2; i <= n; i++)      if (prime(i))          cout<<i<<" ";}


Second program: you only need to remove the code from 2 to SQRT (n ).

# Include <iostream> # include <cmath> using namespace STD; int root (int n) {return (INT) SQRT (float) n);} int prime (int n) {int I; for (I = 2; I <root (n); I ++) // you must call the root () function for each loop. you can apply for a variable, band = root (n) put in front if (N % I = 0) return 0; return 1 ;}int main () {int I, n; n = 1000; for (I = 2; I <= N; I ++) if (prime (I) cout <I <"";}


Third program: through the special test of Division 2, 3 and 5, nearly 3/4 of the square operation is avoided. Statement counting indicates that about half of the input is classified into the sum by the Division of 2, and 1/3 of the remaining input is classified into the sum by the Division of 3, divide the remaining number by 5, and then classify 1/5 of the remaining number into a sum. Second, only the odd number is considered as the possible factor, and the Division test of about half is avoided in the remaining number. It is about twice faster than program P3, but there are more errors than program P3.

#include <iostream>#include <cmath>using namespace std;int root(int n)  {     return (int) sqrt((float) n);  } int prime(int n){        int i, bound;        if (n % 2 == 0)               return 0;        if (n % 3 == 0)               return 0;        if (n % 5 == 0)               return 0;        bound = root(n);        for (i = 7; i <= bound; i = i+2)              if (n % i == 0)                    return 0;          return 1;}int main(){    int i,n;    n=1000;    for(i = 2; i <= n; i++)      if (prime(i))          cout<<i<<" ";} 

The above program did not check the prime number: 2, 3, 5

The improved procedure is as follows:

# Include <iostream> # include <cmath> using namespace STD; int root (int n) {return (INT) SQRT (float) n);} int prime (int n) {int I, bound; If (N % 2 = 0) Return (n = 2 ); // if it is 2, 1 is returned; otherwise, 0 if (N % 3 = 0) Return (n = 3); If (N % 5 = 0) is returned) return (n = 5); bound = root (n); for (I = 7; I <= bound; I = I + 2) if (N % I = 0) return 0; return 1 ;}int main () {int I, n; n = 1000; for (I = 2; I <= N; I ++) if (prime (I) cout <I <"";}


Fourth program: Replace the time-consuming square operation with multiplication, and slightly adjust the third program

Key code:

#include <iostream>using namespace std; int prime(int n){        int i, bound;        if (n % 2 == 0)               return (n==2);        if (n % 3 == 0)               return (n==3);        if (n % 5 == 0)               return (n==5);        for (i = 7; i*i <= n; i = i+2)              if (n % i == 0)                    return 0;          return 1;}int main(){    int i,n;    n=1000;    for(i = 2; i <= n; i++)      if (prime(i))          cout<<i<<" ";}


2. Performance testing tools

In Linux:

Find.-Name "*. c" | xargs WC-l
Time-execute commands and timing

Time find.-Name "*. c" | xargs WC-l // count the command execution time

Real 0m33. 748 s
User 0m0. 772 s
Sys 0m1. 044 s

1) Real Time: the elapsed time from command line execution to running termination;

2) User CPU time: the user CPU time spent on command execution completion, that is, the total execution time of commands in user State;

3) system CPU time: the total execution time of a command in the core state.

The sum of the user's CPU time and system CPU time is the CPU time, that is, the sum of the time the command occupies CPU execution. The actual time is later than the CPU time, because Linux is a multi-task operating system, and the system usually needs to process other tasks when executing a command.

Another issue that needs to be noted is that even if the same command is executed each time, the time consumed is different. The time consumed is related to system running.


Code Performance Detection, counting the number of calls to each function. Can detect which function occupies a large amount of CPU time, thus effectively changing the program.


Iii. Exercise

2) implement a simple sieve method
Of Eratosthenes) to calculate all prime numbers less than N. The main data structure of this Program is an N-Bit Array, and the initial values are true. Every time a prime number is found, the multiples of all the prime numbers in the array are set to false. The next prime number is the next real bit in the array.

Answer: The following C program implements the escret method to calculate all prime numbers smaller than N. The basic data structure is n-Bit Array X, and the initial values are all 1. Each time a prime number is found, all its multiples in the array are set to 0. The next prime number is the bit where the next value in the array is 1. Performance monitoring shows that there are 100000 prime numbers smaller than 9592, and the algorithm has assigned a value of about 2.57n times. Generally, the algorithm performs nlogloglogn assignment. The algorithm analysis involves the prime number density and harmonic number in answer 1.1. The following code is added with performance monitoring:

# Include <iostream> using namespace STD; int main () {int I, P, N; char X [100002]; n = 100000; for (I = 1; I <= N; I ++) x [I] = 1; X [1] = 0; // 1 is not a prime number x [n + 1] = 1; P = 2; while (P <= N) {cout <p <"; for (I = 2 * P; I <= N; I = I + p) X [I] = 0; do P ++; while (X [p] = 0); // obtain the first number marked as 1, which is a prime number }}


3) A simple performance monitoring tool for statement counting increases the count every time a statement is executed. Meeting the requirements of fewer counters can reduce the memory requirements and running time of the monitoring program at the same time. For example, you can associate a counter with each basic block in the program flow chart. You can also use the first law of kikhoff to further reduce the number of counters: if you have an if-then-else statement counter and a then branch statement counter, then the counter of the else branch statement is not required.


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.