C ++ code performance optimization and Code Performance Optimization

Source: Internet
Author: User

C ++ code performance optimization and Code Performance Optimization

For a union operation that can be combined and interchangeable, such as addition or multiplication of integers,

We can divide a combination and Operations into two or more parts, and merge the results at the end to improve performance.

 

Principle:

Common Code can only use one register of the CPU. After segmentation, multiple registers can be used.

When the number of shards reaches one, the registers are used up, and the performance is no longer improved or even decreased.

 

The code is described as follows:

// The general code for (I = 1; I <n + 1; I ++) {res = res limit I ;} // code for (I = 1; I <n; I + = 2) {res1 = res1 then I; res2 = res2 then (I + 1 );}

Int integer addition. The performance test results are as follows:

The addition of integers. The common code runs for 26 s and 18 s after the division.

The floating point computing performance is improved, which is significantly greater than the integer. The multiplication performance is improved, slightly greater than the addition.

 

Complete Test code:

#include <time.h>#include <iostream>#define OPER +#define INIT 0using namespace std;int calc1(int n){    int i;    int res = INIT;    for (i = 1; i < n+1; i++)    {        res = res OPER i;    }    return res;}int calc2(int n){    int i;    int res1 = INIT;    int res2 = INIT;    for (i = 1; i < n; i+=2)    {        res1 = res1 OPER i;        res2 = res2 OPER (i+1);    }    for (; i < n+1; i++)    {        res1 = res1 OPER i;    }    return res1 OPER res2;}typedef int (*FUNC)(int n);int time_test(FUNC calc, int param){    cout << " Result: " << calc(param) << "\t";    time_t t_begin;    time(&t_begin);    for (int i = 0; i < 10000; i++)        for (int j = 0; j < 10000; j++)            calc(param);    time_t t_end;    time(&t_end);    cout << "Time Cost: " << difftime(t_end, t_begin) << endl;}int main(){    cout << "calc1 ";    time_test(calc1, 100);    cout << "calc2 ";    time_test(calc2, 100);    return 0;}

 


C procedural optimization: 20 experiments and talent skills

The author has concentrated his experience in efficient C Programming on 20 skills and explained these skills through experiments, which are concise and easy to understand. The book contains a large number of code examples, allowing readers not only to improve theoretically, but also to easily apply it in practice. · Introduction to algorithms (more than 0.5 million people read the algorithm Bible !) · Thank you for leaving me (Zhang XiaoYu's latest prose) for introducing the content. Starting from the CPU and compiler running mechanism, we will guide you step by step to understand program execution costs and Compiler Optimization Options, I have summarized many skills for optimizing the performance of C Programs and explained them in an experiment. They are concise and easy to understand, which impressed me. The book contains a large number of code examples, allowing readers to understand not only the principles of code optimization, but also easily apply it in practice. "" Is suitable for C language programmers who have a certain degree of foundation to read. The author is proficient in efficient programming. Its C compiler is not only applicable to 16-bit and 32-bit systems, but also supports real-time compilation of video data in GPU. The author has concentrated his experience in efficient C Programming on 20 skills and explained these skills through experiments, which are concise and easy to understand. The book contains a large number of code examples, allowing readers not only to improve theoretically, but also to easily apply it in practice. · Introduction to algorithms (more than 0.5 million people read the algorithm Bible !) · Thank you for leaving me (Zhang XiaoYu's latest prose) for introducing the content. Starting from the CPU and compiler running mechanism, we will guide you step by step to understand program execution costs and Compiler Optimization Options, I have summarized many skills for optimizing the performance of C Programs and explained them in an experiment. They are concise and easy to understand, which impressed me. The book contains a large number of code examples, allowing readers to understand not only the principles of code optimization, but also easily apply it in practice. "" Is suitable for C language programmers who have a certain degree of foundation to read. The author is proficient in efficient programming. Its C compiler is not only applicable to 16-bit and 32-bit systems, but also supports real-time compilation of video data in GPU. The author has concentrated his experience in efficient C Programming on 20 skills and explained these skills through experiments, which are concise and easy to understand. The book contains a large number of code examples, allowing readers not only to improve theoretically, but also to easily apply it in practice. · Introduction to algorithms (more than 0.5 million people read the algorithm Bible !) · Thank you for leaving me (Zhang XiaoYu's latest prose) for introducing the content. Starting from the CPU and compiler running mechanism, we will guide you step by step to understand program execution costs and Compiler Optimization Options, I have summarized many skills for optimizing the performance of C Programs and explained them in an experiment. They are concise and easy to understand, which impressed me. The book contains a large number of code examples, allowing readers to understand not only the principles of code optimization, but also easily apply it in practice. "" Is suitable for C language programmers who have a certain degree of foundation to read.

Which of the following books can be recommended for the optimization of C language (speed and memory) and the programming mode of C language?

Check the Internet. At this level, it is estimated that it is enough for you to do it yourself ,:-)
 

Related Article

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.