Java: using a local variable in double check idiom
<p>Josh Bloch mentions using a local variable in double check idiom for possible performance improvement (EJ, 2nd ed. p284) and says the example code runs about 25% faster on his machine compared to that without a local variable. So the question is, why is it faster? In case of local variable version, it accesses the instance variable 3 times and in normal case it accesses the instance variable 4 times. Is this the reason behind '25%' increase or are there any other reasons?</p>

<p>EDIT: 3 or 4 times access is only when the instance is first created. Thereafter, it's 1 or 2 times.</p>

<p>EDIT2: check the accepted answer for this question to see the example code. I think this is for Java 6. <a href="http://stackoverflow.com/questions/3578604/how-to-solve-the-double-checked-locking-is-broken-declaration-in-java">How to solve the &quot;Double-Checked Locking is Broken&quot; Declaration in Java?</a></p>
