-4

I'm writing the code for a loop sequence and two codes perform the same function but I want to choose the faster one and to know how to identify which one runs faster

for (i=0; i<100; i++) and for (i=0; i<100; ++i)

I've used the two codes and they both perform the same function but I do not not know which is faster

  • Time to benchmark? – Federico klez Culloca Jun 07 '19 at 12:35
  • They perform the same tasks with different order and return value – GalAbra Jun 07 '19 at 12:35
  • considering the evaluated value of i++ or ++i isn't being used, the compiler is supposed to see both as exactly the same. Also, as is said in an answer, don't try to optimize that kind of stuff. Only try to optimize what you profiled as a bottleneck worthy of optimization. – kumesana Jun 07 '19 at 12:37
  • You can find answers to your question here too: https://stackoverflow.com/questions/4831748/is-i-really-faster-than-i-in-for-loops-in-java In Java, no difference in speed. In C++, ++i is faster under some circumstances. – Teik Jun Jun 07 '19 at 12:46

1 Answers1

3

It really doesn't matter. Also: https://stackify.com/premature-optimization-evil/

Rick
  • 935
  • 2
  • 7
  • 22