This is one of the "twenty-eight questions) of" Metamorphology Java programmer interviews "spread online (twenty-eight questions), then the answer given below is
Chapter 28. Program: Two multiplier with the most efficient method is equal to a few? There are C backgrounds, special programs like to ask this question. 2 << 3
The thick look seems to be reasonable, roughly wants to come to 2 << 3 will be shift operation, the shift command is ISHL (right shift) in the Java bytecode, and the hardware instruction on the CPU may be SHL (arithmetic Right shift instruction). In fact, if you are familiar with the assembly language, it is also considered compilation optimization, and 2 << 3 will not use shift operations at all, but in compile time, it is possible to calculate 16.
But if it is written in such a way (INT i = 2; INT J = I << 2;), it is not the same, you can see the Java operation command or assembly instruction generated from the code different write method or assembly instruction.
The bytecode instruction corresponding to the Java code Description INT i = 2 << 3; 0: Bipush 16 is compiled with 2 left to move 3 digits, it can be said that the INT value pops up in the compile time efficiency stack. INT i = 2 * 8 in the local variable of position 1; INT i = 2; INT J = I * 8; 0: IConst_21: IStore_12: iShl 6: iStore_2 constant 2 stack The INT type value is popped in the stack, and the INT type local variable stack constant 3 stack left shift operation in the local variable of position 1 is plugged in, and the INT type value is popped in the result value, and the position is 2 of the local variables INT i = 2; int J = i * 8; 0: iconst_21: iStore_12: iLoad_13: Bipush 85: IMUL 6: ISTORE_2JAVAC generated bytecode operation instruction is imul, Javac is not optimized
So write the code into int i = 2 << 3, and write into int i = 16;, the same, the former has reduced compilation time efficiency
Another example below will be exemplified below, which is actually such an optimization.
Compilation instructions corresponding to C code Description INT i = 2 << 3; MOV DWORD PTR [EBP-4], 10h is also calculated by 2 << 3, 16 (10h), put into memory I = 2 * 8 And INT i = 2; int J = i << 3; MOV DWORD PTR [EBP-4], 2MOV EAX, DWORD PTR [EBP-4] SHL EAX, 3 MOV DWORD PTR [EBP-8], EAX Put 2 in memory ss: [EBP-4] transfer SS: [EBP-4] integer to the EAX register to move the data left in Eax 3-bit and save the result after the shift to memory SS: [EBP -8] INT i = 2; int J = i * 8 automatically optimizes the upper and VC into shift operation, because 8 is 2 integers power int i = 2; int J = i * 9; MOV DWORD PTR [ EBP-4], 2MOV EAX, DWORD PTR [EBP-4] Imul Eax, EAX, 9 MOV DWORD PTR [EBP-8], EAX generated assembly instructions are Imul, because 9 is not 2 integers power
So from the answer is int I = 2 << 3; did not change the efficiency of a little execution because it int i = 16; it is exactly the same. But if it is written into I = 2; int J = i << 3;
Compared
INT i = 2; int J = i * 8:
It is indeed a certain amount of execution efficiency, because i * 8, use Java operation instruction imul to calculate, this is just that JDK's compiler Javac is handled so. JDK is compiled and there is no i * 8 optimization into i << 3.
If you convert the code to a C code, it is also written.
INT i = 2; int J = i * 8:
Compilation instruction code compiled by VC also uses SHL EAX, 3, it and written into int J = I << 3 is all consistent. VC has been given such a generation of performance from the efficiency, and then reflects a sentence in this question. "The programmer with C background specially likes to ask this question." You can learn the programmer with C background. I have played a "little smart" here, but I didn't take a little low, just because he underestimated the optimization of the compiler.
The reason why asking such a problem, they are just based on such a fact that the clock cycle required by the integer multiplication or integer division is much larger than the clock cycle required for the shift operation, and the basic execution time of this instruction is listed below:
Displacement command register 1 bit clock cycle number is 2 integer multiplication IMUL 16-bit register multiplier clock cycle is 128 ~ 154 integer division IDIV 16-bit register clock cycle is 165 ~ 184
Even if the Java compiler is compiled int J = i * 8; when IMUL is used, it is really implementing this code, and the virtual machine JVM conversion cost code is not further optimizing assembly of the application shift operation. It does not know, if necessary, it is necessary to track the execution of Java.exe, even if it is executed, it will be optimized in Java, write int J = i * 8 into int J = i << 3, get one The efficiency of some points is negligible.
Outwate, in Visual C .NET 2003, the code optimization of Intel Pentium 4 and AMD Athlon is added. When using / g7 compile, it can generate faster (but longer) instruction sequences, avoiding the use of IMUL instructions. This instruction has 14 cycles on Intel Pentium 4. Such as i * 9, may be compiled into the following code MOV ECX, DWORD PTR _I $ [ESP-4] MOV EAX, ECXSHL EAX, 3ADD EAX, ECX
Originally should be the Imul Multiplication Directive, with / g7 compilation options, uniformly generated the first left shift 3 digits, plus the original value. What is the online introduction is that I can compile the IMUL instructions that are still generated when I compile in Visual C .NET 2003, compile with / g7 option.
There is an article on the use of Visual C to optimize the code [http://www1.softhouse.com.cn/html/200408/2004083110013100000450.html]
Finally, from the business, such as such a simple demand, A's salary is 8 times the salary of B (B is a low-level slave), in which case you want to write code is written
Salarya = SALARYB * 8; still write
Salarya = Salaryb << 3;
This problem doesn't have to answer.
If there is any situation in the form of INT J = I << 3, there is also, usually in the processing byte, such as all the whole bit left shift 3 digits, low 3 bits and use bytes, but also compare Less 8 times this concept.
I went to a company to interview yesterday, I saw some interview questions (including system analysis questions), I really don't want to do it. I told them when I have been talking about it. I have always don't like to do questions. Often not to pay attention to the problem itself, you have to pay attention to trying to figure out the meaning of the world.
Like the choice questions in the Chinese English exam, if the four answers are
A: AAB B: AAC C: AAD D: 456
General teachers will tell you to exclude the answer d because D is too unimpeded.