Most efficient way of incrementing: += Or i+1 Or i++ ?

Let's discuss which of the following ways of incrementing is the most efficient. Take a look at the following ways of incrementing -

I:

i += 1;

II:


i = i + 5;

III:

i++;

1 & 2 may be the same; but still there exist different styles (I dare say) to increment. What do you think is the most efficient way of incrementing?

Replies

  • Aadit Kapoor
    Aadit Kapoor
    The third way is the best way for incrementing only by 1.
  • Kaustubh Katdare
    Kaustubh Katdare
    Aadit Kapoor
    The third way is the best way for incrementing only by 1.
    You'll have to justify - 'why'? That's the point of this discussion.
  • Anoop Kumar
    Anoop Kumar
    i++; means value of i will increment after the current line.
    ++1 : increment and then put value in current line.
    i+=1 ==> i=i+1;

    Operators are written in very core of language, IMO, all will cost equal.
    Depend on uses and code clarity, they can be used.

    If anyone really want to see different, run a loop of 1000 times and calculate time. Which cost less.
  • Kaustubh Katdare
    Kaustubh Katdare
    Well, I think it'd be an experiment worth doing. Anyone wants to take it up?
  • Vishal Sharma
    Vishal Sharma
    Kaustubh Katdare
    Well, I think it'd be an experiment worth doing. Anyone wants to take it up?
    all the methods give same execution time for me 0.015s
  • Kaustubh Katdare
    Kaustubh Katdare
    Vishal0203
    all the methods give same execution time for me 0.015s
    Interesting. How many decimals can we go down deeper to track time accurately?
  • Anand Tamariya
    Anand Tamariya
    If you realise that computer hardware doesn't understand any of the above three syntax, you'll see the futility of this discussion. By itself, all these statements will be translated to same machine code by the compiler - thereby keeping the execution time same.
  • Abhishek Rawal
    Abhishek Rawal
    Try it in assembly ?
    LOAD @i,r0
    ADD r0,1 //(or use INC r0)
    STORE r0,@i

    BTW doesn't i=i+5 means something like add [ax],5 & i++ is inc ? I am quite sure that assembly will give different execution time. Gotta try in Kate.
  • Kaustubh Katdare
    Kaustubh Katdare
    @#-Link-Snipped-# - That's correct. Will the same hold true for scripting languages?
  • MaRo
    MaRo
    My main concern would be number of instructions used then would be the time in nanoseconds

    I'll test both and come back!
  • ahmed sarfraz
    ahmed sarfraz
    The best way to see an instruction is to test it. We must try to run these and then finally find a solution regarding their difference.
  • KenJackson
    KenJackson
    You didn't mention pre-incrementing "++i;".

    I haven't investigated, but I've read or heard that at least on some architectures pre-incrementing is more efficient than post-incrementing. Though maybe there's only a difference if it's used in a larger expression. The reason has to do with how it maps to instructions.
  • Anand Tamariya
    Anand Tamariya
    Compiler or interpreter would always reduce increment operation the same way for a particular datatype.
  • ahmed sarfraz
    ahmed sarfraz
    i think i++ is efficient brcause it5 is a unary operator. this means it'll increment after its terminator.
  • KenJackson
    KenJackson
    When optimized, all produce exactly the same code in trivial expressions, at least on my x86-64 PC. See for yourself:
    for op in "i += 1" "i = i + 1" "i++" "++i"; do
        echo -e "\nTrying \"$op\""
        echo "int f(int i){ $op; return i;}" | gcc -x c -O2 -c -o f.o -
        objdump -d f.o | tail -n +8
    done
    rm -f f.o
    I think there is a slight advantage to preincrementing in complex expressions (which would require a more complex test) because the processor can fetch, increment, save and use what's in it's register without any juggling.
  • Aadit Kapoor
    Aadit Kapoor
    i++ is the best way to increment by 1 because i++ means only to do +1 whereas i+=1 means first adding +1 to it and then assigning the result to i.So,i++ is more faster than i+=1.
  • Anand Tamariya
    Anand Tamariya
    Aadit Kapoor
    i++ is the best way to increment by 1 because i++ means only to do +1 whereas i+=1 means first adding +1 to it and then assigning the result to i.So,i++ is more faster than i+=1.
    After i++, if you were to print i, won't it be incremented? So the result have to be assigned to i.
  • sookie
    sookie
    I believe the third one will be faster , if we see in terms of Stack. Arithmetic expressions are evaluated using Stack by the Compiler.

    Third one being shortest(i++) will be quick to evaluate by Stack.

    Moreover it do not have extra task of assigning the value to new variable.

You are reading an archived discussion.

Related Posts

Hi frnds, I am a final Computer Science Graduate, we are a group of 5 and we want to start a web development company. Can I get help on structure...
This freak accident apparently happened in 2006. Reported again on CR4 here: https://cr4.globalspec.com/thread/87620/Engineering-Problem-or-Stupidity?frmtrk=cr4digest Interesting comments. The actual story is here: https://www.snopes.com/photos/accident/hoecrash.asp
Lava Mobiles International has launched a new smartphone in India with LAVA 3G 402+, a revamped version of the Lava 3G 402 Android phone. The newly upgraded phone features a...
The Samsung Galaxy Grand 2 was launched by the Korean company in November and today it if officially unveiling in India. Bigger in size and processing power than it's younger...
Aston Martin Rapide S has been launched in India, and this luxury car shall be replacing Aston Martin Rapid in global circuits. There's a reason why. This four-doored car is...