i++
and ++i
are bytecode-equivalent (Java)Idk if you've heard of performance differences between post-increment (e.g. i++
) and pre-increment (e.g. ++i
). The idea is that post-increment needs to be implemented as tmp = i ; i = i + 1; return tmp
whereas pre-increment can be implemented as i = i + 1; return i
and so is more efficient. But I was thinking about the following question: In Java, is there a performance difference between i++
and ++i
if the result is never used? How can I be sure that i++ [return value unused]
is always executed as just i = i + 1
? I found this stackoverflow answer that uses the following neat technique: Just write two programs that are identical except for i++
vs ++i
, and observe that the java bytecode produced (.class file) is exactly the same. So no semantic understanding of the bytecode is required, just (classfile1 diff classfile2
or md5sum classfile1 ; md5sum classfile2
)