Is there any performance difference between using int a=a+1 and a++ in Java?
If so which is better and why? Could you briefly explain me to understand this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Looking at the generated bytecode:
generates (use
javap -c classname)So using (jdk1.6.0_18):
creates
whereas
all result in
However, doing a rough performance test on my laptop resulted in next to no difference in the runtime between the two (sometimes ++x was quicker, sometimes x=x+1 was quicker), so I wouldn’t worry about the performance implications.