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

Kaustubh Katdare

Kaustubh Katdare

@thebigk Oct 26, 2024
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

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Aadit Kapoor

    Aadit Kapoor

    @aadit-kapoor-EBxnXT Dec 22, 2013

    The third way is the best way for incrementing only by 1.
  • Kaustubh Katdare

    Kaustubh Katdare

    @thebigk Dec 22, 2013

    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

    @anoop-kumar-GDGRCn Dec 22, 2013

    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

    @thebigk Dec 22, 2013

    Well, I think it'd be an experiment worth doing. Anyone wants to take it up?
  • Vishal Sharma

    Vishal Sharma

    @vishal-pysGmK Dec 22, 2013

    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

    @thebigk Dec 22, 2013

    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

    @anand-tamariya-DnfjEX Dec 22, 2013

    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

    @abhishek-fg9tRh Dec 22, 2013

    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

    @thebigk Dec 22, 2013

    @#-Link-Snipped-# - That's correct. Will the same hold true for scripting languages?
  • MaRo

    MaRo

    @maro-Ce3knx Jan 7, 2014

    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

    @ahmed-Dx1LlC Jan 7, 2014

    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

    @kenjackson-mBf7HF Jan 7, 2014

    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

    @anand-tamariya-DnfjEX Jan 7, 2014

    Compiler or interpreter would always reduce increment operation the same way for a particular datatype.
  • ahmed sarfraz

    ahmed sarfraz

    @ahmed-Dx1LlC Jan 8, 2014

    i think i++ is efficient brcause it5 is a unary operator. this means it'll increment after its terminator.
  • KenJackson

    KenJackson

    @kenjackson-mBf7HF Jan 9, 2014

    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

    @aadit-kapoor-EBxnXT Jan 9, 2014

    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

    @anand-tamariya-DnfjEX Jan 9, 2014

    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

    @sookie-T06sFW Jan 16, 2014

    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.