C Register keyword-the fastest keyword

Source: Internet
Author: User
Tags modifiers

Register: This keyword requests the compiler to make the variables available in the CPU internal registers as much as possible, rather than through memory addressing, to improve efficiency . note is as far as possible, not absolute . You think, a CPU register is just a few or dozens of, if you define a lot of register variables, it may not be exhausted can not all put these variables into the register bar, the wheel may not be your turn.

first, the emperor's side of the Small eunuch----register

don't know what a register is? Have you seen the eunuch? No? I didn't actually. It doesn't matter if you've seen it, it's a big hassle. ^_^, we have seen the costume play, those emperors to read Memorial, the minister is always first to the emperor next to the memorial, small eunuch, and then handed over to the emperor comrade. This little eunuch is just a transit point and has no other function.
Well, then we'll associate our CPU. The CPU is not our emperor comrade? The minister is the equivalent of our memory, and the data is taken out of him. The eunuch is our register (this is not the cache area of the CPU first). The data is taken out of memory and placed in the register, then the CPU is then read data from the register to process, after processing the same data through registers into memory, theCPU does not directly deal with the memory . Here to illustrate the point is: The Small eunuch is the initiative from the Minister took memorial, and then take the initiative to the Emperor comrade, but register not so consciously, it never take the initiative to do things. An emperor may have a number of small eunuchs, then a CPU can also have a lot of registers, different models of the CPU has a register of the same amount.
Why so much trouble? speed! Just because of the speed. A register is actually a small piece of storage space, but its access speed is much faster than memory. Water tower first get the moon, it is very close to the CPU, the CPU one hand to get the data, than in such a large piece of memory to find the data on an address is much faster? It was asked that since it is so fast, our memory hard disk is changed to register. What I want to say is: You are so rich!

Ii. examples

The register modifier implies that the corresponding variables of the compiler will be used frequently and, if possible, should be kept in the registers of the CPU to speed up its storage. For example, the following memory block copy code,

  

  

#ifdef nostructassign

memcpy (d, S, L)

{

Register char *d;

Register char *s;

Register int i;

While (i--)

*d++ = *s++;

}

#endif

   Iii. note points using the Register modifier

However, there are several limitations to using the Register modifier.

First, theregister variable must be a type that can be accepted by the CPU . This usually means that the register variable must be a single value , and the length should be less than or equal to the length of the integral type . However, some machine registers can also store floating-point numbers.

Second, because the register variable may not be stored in memory, "&" cannot be used to obtain the address of the register variable .

Because the number of registers is limited, and some registers can only accept certain types of data (such as pointers and floating-point numbers), the number and type of register modifiers that really work depends on the machine that runs the program, and any extra register modifiers are ignored by the compiler.

In some cases, saving a variable in a register will slow down the program. Because the occupied registers can no longer be used for other purposes , or the variable is not used enough, it is not enough to load and store the additional overhead of the variable.

The early C compiler does not keep the variable in the register unless you command it to do so, and the Register modifier is a valuable addition to the C language. However, with the progress of compiling programming techniques, when deciding which variables should be stored in registers, now the C compiler environment can make a better decision than the programmer . In fact, many compilers ignore the Register modifier because, although it is perfectly legal, it is merely a hint rather than a command .

C Register keyword-the fastest keyword

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.