A little summary of optimization

Source: Internet
Author: User

Memory aliasing (memory alias used)

The compiler must assume that different pointers may point to the same location in the storage. This creates a major impediment to optimization.

For example:

void Twiddle1 (int *xp, int *yp)

{

*xp + = *YP;

*xp + = *YP;

}

void Twiddle2 (int *xp, int *yp)

{

*XP = 2 * (*YP);

}

It looks like Twiddle1 is implementing the same functionality as Twiddle2, and Twiddle2 should be Twiddle1 's optimized version because twiddle2 only needs to access one XP and one YP at a time, while Twiddle1 uses it two times.

However, consider one of the following:

int t;

Twiddle1 (&t, &t);

Twiddle2 (&t, &t);

Do they get the same result, no. This is what the compiler must take into account when doing optimization, the compiler does not fully understand the programmer's intentions, so only the current code conservative optimization.

The same examples are:

int counter = 0;

int f (int x)

{

return (COUNTER+X);

}

int F1 (int x)

{

return f (x) +f (x);

}

int f2 (int x)

{

return 2*f (x);

}

how to represent the performance of a program-cpe/

Cpe:cycles per element, the same number of concurrent elements.

How to understand, such as an array int array[50], which is used to compute in function f (), the last F () uses CPU clock 100, then the CPE of function f () is 100/50=2.0;

Why not take the number of cycles per cycle instead of the same number of times for each element, because there may be loops unfolding.

How to eliminate the low efficiency of loops

Ø reduce unnecessary function calls:

The Vec_length in the for (i = 0; i < Vec_length (v) i++) can be completely placed outside the loop body.

Ø eliminates unnecessary memory references

Such as

for (i = 0; i < length; i++)

{

*dest = *dest + data[i];

}

You can use local variables to perform operations, and finally assign values to dest.

Ø for circulation, let software flow

Note that the prerequisite for software to flow is that there is no judgment statement in the body of the loop, or an IF. So to get the code to execute faster, try to move the judgment statement out of the loop.

reduce the overhead of floating-point operations

In general, the speed at which a processor makes fixed-point operations is as fast as a floating-point operation (a dedicated floating-point processor exception). As in DM642, the speed of fixed-point operation is 10 times times the speed of floating-point operation. Therefore, in the process of dealing with floating-point numbers, the first conversion to fixed-point after the operation of the assignment will be a great performance.

whether the array should be converted to pointer code

Based on experience, array code will be preferable to pointer code, we have seen compilers that apply very advanced optimizations to the array code, while only minimal optimizations are applied to the pointer code. And the array code is more readable.

reasonable use of cache

For DM642, the size of the L2 cache can be set, but if the cache is fully open, all L2 SRAM will be exhausted. This makes it impossible to use the technology such as DMA two buffer. But if it's not open, it has a very big impact on the speed of the program.

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.