Unlikely and likely

Source: Internet
Author: User
It is mainly related to the processor. At present, the processor is a pipeline. Some of them have multiple logical operation units, and the system can take multiple commands in advance for parallel processing. However, when a jump occurs, you need to re-obtain the command, which is slower than not re-execute the command. Therefore, when the system runs the unlikely macro, the redirection is reduced and the index is retrieved again.

Unlike assert (), assert () is used to determine a value. For example, if the pointer is null, the program will exit directly. Unlike unlikely,

The accuracy of conditional branch prediction is increased. The CPU loads the subsequent commands in advance, and predicts and loads the commands of a certain branch in case of conditional transfer commands. Unlikely indicates that this condition rarely occurs. likely indicates that this condition occurs in most cases. The compiler generates code to optimize the CPU execution efficiency.

 

Likely () and unlikely () can be seen everywhere in the 2.6 kernel. Why should they be used? What are the differences between them?

First, make it clear:

If (likely (value) is equivalent to If (value)

If (unlikely (value) is also equivalent to If (value)

That is to say, likely () and unlikely () are the same from the perspective of reading and understanding the code !!!

These two macros are defined in the kernel as follows:

# Define likely (x) _ builtin_exact CT (x), 1)

# Define unlikely (x) _ builtin_exact CT (x), 0)

_ Builtin_ct CT () is provided to programmers by GCC (version> = 2.96) to provide "branch transfer" information to the compiler, so that the compiler can optimize the code, to reduce the performance degradation caused by command redirection.

_ Builtin_ct (x), 1) indicates that the value of X is more likely to be true;

_ Builtin_exact CT (x), 0) indicates that the value of X is more likely to be false.

That is to say, when likely () is used, the chance of executing the statement after if is greater. When unlikely () is used, the chance of executing the statement after else is greater.

It is mainly related to the processor. At present, the processor is a pipeline. Some of them have multiple logical operation units, and the system can take multiple commands in advance for parallel processing. However, when a jump occurs, you need to re-obtain the command, which is slower than not re-execute the command. Therefore, when the system runs the unlikely macro, the redirection is reduced and the index is retrieved again.

Unlike assert (), assert () is used to determine a value. For example, if the pointer is null, the program will exit directly. Unlike unlikely,

The accuracy of conditional branch prediction is increased. The CPU loads the subsequent commands in advance, and predicts and loads the commands of a certain branch in case of conditional transfer commands. Unlikely indicates that this condition rarely occurs. likely indicates that this condition occurs in most cases. The compiler generates code to optimize the CPU execution efficiency.

 

Likely () and unlikely () can be seen everywhere in the 2.6 kernel. Why should they be used? What are the differences between them?

First, make it clear:

If (likely (value) is equivalent to If (value)

If (unlikely (value) is also equivalent to If (value)

That is to say, likely () and unlikely () are the same from the perspective of reading and understanding the code !!!

These two macros are defined in the kernel as follows:

# Define likely (x) _ builtin_exact CT (x), 1)

# Define unlikely (x) _ builtin_exact CT (x), 0)

_ Builtin_ct CT () is provided to programmers by GCC (version> = 2.96) to provide "branch transfer" information to the compiler, so that the compiler can optimize the code, to reduce the performance degradation caused by command redirection.

_ Builtin_ct (x), 1) indicates that the value of X is more likely to be true;

_ Builtin_exact CT (x), 0) indicates that the value of X is more likely to be false.

That is to say, when likely () is used, the chance of executing the statement after if is greater. When unlikely () is used, the chance of executing the statement after else is greater.

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.