Record as you want (two problems in code optimization) and record optimization as you want

Source: Internet
Author: User

Record as you want (two problems in code optimization) and record optimization as you want


[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]


Two problems have been found recently during code optimization. One is the problem of global variables, and the other is the issue of compiling out of order. Because we didn't notice these problems at the beginning, we have to put more energy into it now. To illustrate these two problems, we can write an example to illustrate them.

// gcc -O2 -S test.c#include <stdio.h>int a;int b;void fun1(){a = 1;}void fun2(){while(!a);}void fun3(){b = a + 1;a = 0;}int main(int argc, char* argv[]) {return 1;}

First, let's look at the code in fun2. What is the global variable in gcc-O2-S test. c? In addition, will the two lines of code in fun3 be out of order? It is not difficult to do this. Just open the optimization and check the corresponding Assembly file.

.p2align 4,,15.globl fun2.typefun2, @functionfun2:movla, %eaxpushl%ebpmovl%esp, %ebptestl%eax, %eaxje.L7popl%ebpret.L7:jmp.L7.sizefun2, .-fun2.p2align 4,,15.globl fun3.typefun3, @functionfun3:movla, %eaxpushl%ebpmovl%esp, %ebpmovl$0, aaddl$1, %eaxmovl%eax, bpopl%ebpret.sizefun3, .-fun3.p2align 4,,15

Looking at this Assembly code, we can find that the loop in fun2 is actually performed once, and the endless loop is generated after the judgment is completed. As for fun3, we can find that a = 0 is actually executed in advance. In general, there is no problem with code out of order, but if I/O addresses are executed, there is a problem. The only method is to add some workarounds. As for the effect, let's look at the effect of disassembly.

#include <stdio.h>volatile int a;int b;#define COMPILE_BARRIER() __asm__ __volatile__ ("":::"memory")void fun1(){a = 1;}void fun2(){while(!a);}void fun3(){b = a + 1;COMPILE_BARRIER();a = 0;}int main(int argc, char* argv[]) {return 1;}



Website code optimization?

Code optimization refers to equivalent (without changing the running result of the program) Transformation of the program code. The program code can be the intermediate code (such as the four-element code) or the target code. The equivalent meaning is that the running result of the transformed code is the same as that of the code before the transformation. Optimization means that the final generated target code is short (shorter running time and smaller occupied space), and the time-space efficiency is optimized. In principle, optimization can be performed at all stages of compilation, but the most important one is to optimize the intermediate code, which is not dependent on a specific computer.
Optimization that can be performed during compilation can be divided by stages: optimization can be performed at different stages of compilation, which can be divided into intermediate code level 1 and target code level 1 optimization. Programs involved in optimization can be divided into local optimization, cyclic optimization, and global optimization for the same stage. The basis for optimization is Data Flow Analysis and Control Flow Analysis for code. Such as dividing the DAG, searching for loops, and analyzing the variable's fixed value points and reference points. The most commonly used code optimization techniques include deleting redundant operations, repeating the code without changing the code, weakening the strength, changing the cyclic control conditions, merging known quantities and duplication propagation, and deleting useless values.
Reference: www.lygsem.com

Java compiler code optimization

In theory, you can find a lot by yourself.
For example, you call another method B from method.
We know that variables with the same name can be created in a and B, for example, int I = 0. The root cause of this phenomenon is that the call of a method will be interrupted. After the interrupt is generated, the cpu will perform on-site protection, including the pressure of the variable and so on, that is, the resources related to method a are pushed to the stack, while those related to Method B are placed at the top of the stack, only the stack TOP resources can interact with the cpu (the variable I in method a is protected). When Method B ends and leaves the stack, method a returns to the top of the stack, obtain the running result of Method B and continue running.

Ah, it's awkward. Call, interrupt, pressure stack, and so on. You can say that you don't consume resources at all. That's impossible. It will consume a lot of resources, although it is very insignificant. One of the functions of the compiler's optimization process I know is to make these optimizations. Originally, method a has a total of 10 sentences. If you want to write only one sentence, then 2nd sentences are written as Method B and 3rd sentences are written as method c..., and then nested calls are made in sequence. After such source code is optimized by the compiler, you can directly write 10 sentences as a result, that is, the optimization is implemented to a certain extent.

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.