0

I have a best practises kind of question,

I have written an animated analog clock class which uses the Swing Timer to repaint the position of the second, minute and hour hands onto a clock which is called once per second.

I use three int variables to store the temporary values of the current secs, mins and hours.

If the paintComponent() method is the only method which uses these three variables would it be more efficient for the processor load of the program if the variables were declared globally, so that they are created once and simply edited every time that the timer calls paintComponent(), or would it be better to declare the variables locally in the paintComponent() method's scope each time as used?

Saro Taşciyan
  • 5,210
  • 5
  • 31
  • 50
Unl1ght
  • 107
  • 1
  • 6
  • 2
    You want to turn what are by nature local variables into global ones? A *bad* idea on every count: maintainability, readability, performance, you name it. – Marko Topolnik Jan 10 '14 at 15:55

1 Answers1

1

Doesn't matter one way or the other. The local variable will be allocated on the stack, which will be create no matter what. The additional storage for the variable just changes the delta of how much is allocated from the stack (i.e. SP = SP + 10 vs SP = SP + 6). The global will be allocated once, but, again, the stack allocation doesn't "cost anything".

So there's no difference here.

Simply put you should write "idiomatic" Java, and let the JIT handle edge case optimizations such as promoting something like a local variable.

Will Hartung
  • 115,893
  • 19
  • 128
  • 203
  • But that doesn't answer the question about performance ("processor load"): Is creating the variables each time more expensive than an additional effort to access fields compared to local variables? I agree with your conclusion however, especially in this case. – Hauke Ingmar Schmidt Jan 10 '14 at 16:02
  • this does answer the question, there is no concern about "processor load" whatever that means, memory accesses are memory accesses on the stack. –  Jan 10 '14 at 16:04
  • @JarrodRoberson There is definitely a difference in performance, and it is in favor of *local variables* due to locality of reference and the resulting cache-friendliness. You can [check out this answer](http://stackoverflow.com/questions/20807388/why-is-the-new-keyword-so-much-more-efficient-than-assignment/20807788#20807788) where we had two cases which differred in the exact equivalent of the difference between the two approaches proposed here. – Marko Topolnik Jan 10 '14 at 16:11
  • @JarrodRoberson It is stated explicitly in the question so of course there is a concern about it. It is the _one_ thing asked in the question ("would it be more efficient for the processor load of the program if"). It may not have an effect but then _that_ would be an answer. The terminology may be a little bit off, but I think it is obvious what is meant. "Additional storage" was not part of the question. – Hauke Ingmar Schmidt Jan 10 '14 at 18:23