This is for an android game.
Suppose I have a function that is called several times for second and do some calculations involving an arraylist (or any other complex objects for what matter).
Which approach would be preffered?

As a good rule: do not worry too much about performance until you have performance issues. When that happens run a performance profiler and find out where. You don't want to optimize one part of the program and find out later that your bottleneck is somewhere different and you wasted hours optimizing something that didn't need it. Also it is better to finish the project and have to tweak performance, then tweak performance at as you go and feel like you will never finish. I have seen project go unfinished because too much effort was put into micro-optimizations and not adding features.
–
NeedsnamingNov 21 '12 at 18:48

@Needsnaming As good and right you tip is, if I can begin by having an optimal design, it is better than go back and refactor a good chunk of code, isn't it? I mean, I'm not really going out of my way to make the arraylist global, I just don't know if it is actual improvement.
–
petervazNov 23 '12 at 17:20

If you need to optimize this because of performance issues which is possible since you need to create this array several times every second, use a global one.
I am not sure why you would clear it? Where does the data come from? Could you not overwrite the existing data without clearing explicitly?

I use the list to hold chunks of gems in a match-3 puzzle game. each time there is a drop, I call this function for every gem and check neighboors, adding matches to the list. at the end of the function I count the matches and don't need the list anymore. As it is, I have no performance issues (at least on my galaxy tablet), but I'm curious to know if this is a legitim performance gain.
–
petervazNov 23 '12 at 17:28

well.. The best way to improve performance is profiling. You check which function takes up the most CPU time and then modify to be as fast as possible. I think this issue could be detrimental to performance only if it gets out of hand. You need to check how much memory is used for this data structure. I'm thinking very little. If it did take up more memory, it could have had a negative effect on performance.
–
zehelvionNov 23 '12 at 19:16