0

I declared one variable inside for loop and compiled the class. In next class I did the same but this time I just declared variable outside loop. In both cases byte code generated was same but when jvm executed code for both performance readings were different.

My Question is :

  1. Does Same byte code means same source code ? (I think no because I had different code each time)

  2. How does same JVM gives different performance measurements for same bytecode. (I have captured data for each bytecode several times hence I am asking this question. Same JVM is also used and there were no other applications running on system)

There are so many links available for variable declaration inside and outside loop in java.

I am really waiting for a strongest answer !!!

10
  • 8
    1. Yes. Well equivalent anyway. 2. There are lies. Damn lies. And statistics. Commented Dec 24, 2013 at 6:54
  • you could have different source code if your code has different whitespace it and comments which don't affect the byte code. There is probably other things too. Commented Dec 24, 2013 at 6:56
  • There can be quite a big difference declaring variables inside/outside loops unless the compiler optimized it away, they shouldn't be generating the same bytecode. If the bytecode is the same no difference exists in the performance of the code given that everything else is left constant (jvm version, OS, system load, etc). Commented Dec 24, 2013 at 6:56
  • 2
    There's also the JIT behavior. Cache hits vs misses, was it a cold run or a warm run... see #2 above. Commented Dec 24, 2013 at 6:58
  • 1
    Your sample size is two? What was your measured difference? Also, a man with a watch knows what time it is. A man with two watches is never sure. Commented Dec 24, 2013 at 7:07

1 Answer 1

3

Does Same byte code means same source code ? (I think no because I had different code each time)

Not identical. For example, comments and white-space in the source code do not appear in the bytecodes. And neither do names of variables, and other things. (Variable names typically do appear elsewhere in the classfile though.)

Also, it is possible for two equivalent expressions of the same algorithm to compile to the same bytecodes.

How does same JVM gives different performance measurements for same bytecode.

I can think of a number of reasons:

  • It could simply be an artefact caused by poor design of your benchmark. A classic Java benchmarking mistake is to not properly take account of "JVM warmup" effects.

  • It could be due to differences in the way you are calling the code.

  • It could be due to differences in the inputs. (The JIT makes use of statistics gathered early in the execution to decide how to optimize the code. Different inputs could result in different execution stats ... and differently optimized code.)

  • It could be due to other things happening on the system while you were doing your testing.


Here are my performance readings in milliseconds Outside declaration of variable 10076ms Inside declaration of variable 10100 ms Outside declaration of variable 10130ms Inside declaration of variable 10274ms

Differences that small could easily be due to hardware factors / environmental factors; e.g. CPU frequency scaling, or disc rotational latency. Note that the difference between 10076 and 10100 is SMALLER than the difference between 10076 and 10130.


If you want a definitive answer, you will need to provide the complete code for your tests so that people can check for problems, and attempt to reproduce your results.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.