• Kaustubh

MemberDec 22, 2013

## 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?
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
• MemberDec 22, 2013

The third way is the best way for incrementing only by 1.
Are you sure? This action cannot be undone.

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.
Are you sure? This action cannot be undone.
• MemberDec 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.
Are you sure? This action cannot be undone.

Well, I think it'd be an experiment worth doing. Anyone wants to take it up?
Are you sure? This action cannot be undone.
• MemberDec 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
Are you sure? This action cannot be undone.

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?
Are you sure? This action cannot be undone.
• MemberDec 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.
Are you sure? This action cannot be undone.
• MemberDec 22, 2013

Try it in assembly ?
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.
Are you sure? This action cannot be undone.

@#-Link-Snipped-# - That's correct. Will the same hold true for scripting languages?
Are you sure? This action cannot be undone.
• MemberJan 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!
Are you sure? This action cannot be undone.
• MemberJan 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.
Are you sure? This action cannot be undone.
• MemberJan 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.
Are you sure? This action cannot be undone.
• MemberJan 7, 2014

Compiler or interpreter would always reduce increment operation the same way for a particular datatype.
Are you sure? This action cannot be undone.
• MemberJan 8, 2014

i think i++ is efficient brcause it5 is a unary operator. this means it'll increment after its terminator.
Are you sure? This action cannot be undone.
• MemberJan 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.
Are you sure? This action cannot be undone.
• MemberJan 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.
Are you sure? This action cannot be undone.
• MemberJan 9, 2014