Tip of the week#3: Use of ++ and —
++ and — are the unary operators for incrementing and decrementing the variables.
Let us take an example.
Example 1:
int i = 10; i = i + 1;
Example 2:
int i = 10; i++;
Although the above two example perform the same task, there is a significant difference (in ms) in the time required to execute the above two.
The execution of the instruction “i = i + 1” takes more time than “i++”.
Unary operators produce fewer instructions and run faster.
Awesome!
I needed this difference.
Can u plz elaborate it; the reason?
I want to know that how does i++ takes fewer instructions as compared to i+1?
Plz do reply within a day!
Thanks!
For a give statement, there is a specific internal operations that needs to be processed by compiler.
There is a difference between i = i + 1 and i++.
In “i = i + 1”, first instruction (i + 1) would be executed and then the result is assigned to the variable i.
In i++, the value of i itself gets incremented and there is no assignment operator involved. Hence i++ executes faster than i = i + 1
Hope this clarifies.
Thanks,
LearnCOnline Team
Yeah this is relevant for me.
Thanks for the clarification.
Regards:-
Loveleen Sharma.