+ 18

Which is more efficient n=n+1 or n++?

Whats the major difference between these statements as both of them do the same operations.

9th Nov 2019, 7:09 AM
toma_hawk
toma_hawk - avatar
52 Respostas
+ 23
The second has two characters less.
9th Nov 2019, 8:49 AM
Sonic
Sonic - avatar
+ 12
Most compilers optimizes the code you write, and those two will generate the same assembly code. In that case, it would be more efficient to just write n++ or ++n than n=n+1.
9th Nov 2019, 8:27 AM
Kulisap
Kulisap - avatar
+ 9
Modern compilers generate the same assembly code hence practically its no difference, since its just an INC instruction.
9th Nov 2019, 8:34 AM
toma_hawk
toma_hawk - avatar
+ 5
Since the compiler selects the most efficient route of execution, the n=n+1 would be automatically converted to n++. There is no real difference between these as far as the compiler is concerned.
10th Nov 2019, 12:08 PM
Misty Collins
Misty Collins - avatar
+ 4
Both are same.... in terms of efficiency as both statements can be considered as having constant time complexity...
9th Nov 2019, 9:10 AM
Saurabh B
Saurabh B - avatar
+ 3
AFAIK n++.... Since there is increment and reassignment in n=n+1
11th Nov 2019, 3:44 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 2
both are the same but n++ is easy to write
9th Nov 2019, 11:16 AM
El Mehdi El Ouakifi
+ 2
They are the same but you should be careful about pre and post increment
9th Nov 2019, 5:50 PM
Emad Hamdy
Emad Hamdy - avatar
+ 2
With: n++ ; You write less code and I think it's more intelligent way of code. šŸ‘ŠšŸ‘Š
10th Nov 2019, 12:47 AM
BRO
BRO - avatar
+ 2
n++ is more efficient bcoz there is least time to load, compile & output.
10th Nov 2019, 1:11 AM
Shashi Ranjan Kumar
Shashi Ranjan Kumar - avatar
+ 2
n++
10th Nov 2019, 1:49 AM
Akil
+ 2
According to 'c' and 'c++' pre and post decrement or increment operator is good because it feels handy with program Many languages does not support 'n++' type of format like python..,we have to take care about themšŸ™‚
10th Nov 2019, 3:01 AM
Dipak Bhatt
Dipak Bhatt - avatar
+ 2
n++ Better then the other
10th Nov 2019, 6:07 AM
Rishu Dubey
Rishu Dubey - avatar
+ 2
As it takes less space
10th Nov 2019, 6:07 AM
Rishu Dubey
Rishu Dubey - avatar
+ 2
N=N+1 is efficient
10th Nov 2019, 5:04 PM
Akintunde Taofeek
Akintunde Taofeek - avatar
+ 1
You could also compare them with n+=1
9th Nov 2019, 4:16 PM
Logomonic Learning
Logomonic Learning - avatar
+ 1
both are same but n++ is better
9th Nov 2019, 7:24 PM
anvar del
anvar del - avatar
+ 1
n++ is most usefull and easy
9th Nov 2019, 10:26 PM
Priyabrata Pal
Priyabrata Pal - avatar
+ 1
n++
10th Nov 2019, 12:03 AM
Daniel
Daniel - avatar
+ 1
++n is the good habit to using in programming
10th Nov 2019, 2:55 AM
Pawan Kushwah
Pawan Kushwah - avatar