4

Is the first opeartion faster than the second one ?

  u+= (u << 3) + (u << 1) //first operation
  u+= u*10  //second operation 

Basically both of them does the same thing that is u= u+(10*u) But i came to knew that first operation is faster than second . Does the cpu time when operation + different from * . Is multiplication by 10 actually equivalent to 10 addition operations being performed ?

Invictus
  • 4,028
  • 10
  • 50
  • 80
  • 6
    If by faster, you mean faster to understand, then `u+= u*10` is the one! – assylias Apr 30 '12 at 14:54
  • 9
    Depending on the context, `u *= 11` may be even easier to understand. –  Apr 30 '12 at 14:55
  • 2
    This question is a duplicate except for the fact that is asks only about Java and .NET (the answer applies to C and C++ as well, though): http://stackoverflow.com/questions/1168451/is-shifting-bits-faster-than-multiplying-and-dividing-in-java-net the bottom line is that you should write the code that expresses your *intent* - compilers are very, very good today at optimizing multiply or divide operations by constants. And they get the stupid stuff (like correctly handing signed arithmetic) right, which may not always be the case if you do the shifts manually. – Michael Burr Apr 30 '12 at 15:12
  • @MichaelBurr Thanks .. Does that mean i should not use shift operations manually in my code and let compiler does all that stuff as it could lead to error in handling shift arithmetic? – Invictus Apr 30 '12 at 15:34
  • @Ritesh: I think that if you're performing a multiply operation, then express it that way in the code. If it's a multiply by a constant, compilers today are good art reducing that to shits and adds if those operations will be faster. You should only worry about what the compiler is spitting out in code that you *know* (such as by measurement) to be critical. At that point look at the assembly and decide if something else needs to be done. – Michael Burr Apr 30 '12 at 15:56

3 Answers3

15

It depends on the capabilities of the underlying CPU, and of the compiler.

Any decent compiler should optimise u*10 into the appropriate bit-shift operations if it believes they would be faster. It may not be able to do the opposite. So always write u*10 if you mean u*10, unless you know you're working with a bad compiler.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
6

Use a profiler and observe the generated machine code.

It is unlikely that there will be any difference in execution time as the compiler will probably optimise both to the same machine code.

I just ran quick profile test on the 2 in order to assert my claims. I made 2 small binaries (one for each operation) and timed the execution for processing 10e6 integer values. Both report ~38 milliseconds on my machine (mac i7 using g++). Therefore, it is safe to assume that both end up as an identical number of operations in the end. It is likely that the result will be the same for other compiler/processor combinations.

. . . if both give identical performance use:

u+= u*10  //second operation 

.. just because it is a lot easier to understand at a glance.

learnvst
  • 15,455
  • 16
  • 74
  • 121
4

Depends on the compiler's translation and the processor. Some processors have multiplication units so that actually multiplying only takes one instruction.

So far the first requires at least 3 instructions.

When in doubt, profile.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154