Loading...
Logo
Processing Forum
This is a rather general programming question:
Is it preferable to create temporarily used variables locally only, or is there a speed-benefit in declaring them global (or semi-global within their routine/class etc.)
 
In short, what are the benefits and disadvantages of the following versions:
Copy code
  1. void draw()  {
  2.     float f;
  3.     for (int i=0;i<1000;i++){
  4.       f = random(0,1);
  5.     }
  6.   }
Or:
Copy code
  1. float f;
  2. void draw()  {
  3.     for (int i=0;i<1000;i++){
  4.       f = random(0,1);
  5.     }
  6.   }

Or:

Copy code
  1. void draw()  {
  2.     for (int i=0;i<1000;i++){
  3.      float f;
  4.       f = random(0,1);
  5.     }
  6.   }

 

Replies(5)

Generally, try to avoid using 'global' variables unless you have good reason to do so (e.g. the variable's contents are needed by more than one method in your sketch). This will help reduce the chances of inadvertently changing the contents of them in your code. So your second choice is one to avoid.

I generally try to make variables as local in scope as possible, so your third option is the one I'd go for. There might be a tiny overhead in declaring a variable inside a loop as you do in your third option, but I would argue that this is vastly outweighed by the advantages in program clarity (and in your example, the computational cost of generating a random number).

A very minor point, but I think the code in the third option would be a little clearer if you declare and initialise the variable on the same line:

float f = random(0,1);

If you are creating objects that are known to have a big overhead when they are declared (and good API documentation should make it clear when this is the case), then these should be done outside of loops if that is practical, but for the reasons above, should be 'local' inside methods unless you have good reason to share them between methods.
I agree fully with your answer. But " There might be a tiny overhead in declaring a variable inside a loop as you do in your third option" is a common misconception (which I shared for a time...). Java compiler is highly optimizing and you probably won't find a difference in speed (it can even put itself a constant declaration out of a loop if necessary).
Indeed - that is why I said 'tiny'. In my first draft I actually said 'very very tiny' but I thought that was over-egging it somewhat.

I raised the issue because if the variable is an object with a complex constructor, it could become important.
Just picking on to that: This means that "locale vs. global" is an issue which strongly depends on the capabilities of the specific compiler rather than a "general" rule for good programming (in all languages) ?
No, I don't really think so (at least in most cases). I would prioritise good programming practice over compiler optimisation. Keeping code clean and elegant will save you much more time than second guessing compiler efficiencies. In Joshua Bloch' s excellent book Effective Java, item 55 'Optimise Judiciously', he quotes Jackson's well known programmers' aphorism

We follow two rules in the matter of optimisation:
Rule 1: Don't do it.
Rule 2 (for experts only): Don't do it yet - that is, not until you have a perfectly clear and unoptimised solution. 
He suggests that we should strive to write good programs rather than fast ones. My experience suggests this is very good advice. Good programs will be fast. Bad programs are unlikely to be so.