Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

local:

private void doStuff(){
    ArrayList<Type> XList = new ArrayList<Type>();
    // do stuff with list
}

global:

private ArrayList<Type> XList = new ArrayList<Type>();
private void doStuff(){
    XList.clear();   
    // do stuff with list
}
share|improve this question
5  
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. –  Needsnaming Nov 21 '12 at 18:48
    
@Needsnaming - I like that tip! –  TheLima Nov 21 '12 at 19:26
    
@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. –  petervaz Nov 23 '12 at 17:20
add comment

2 Answers

up vote 3 down vote accepted

If you don't need the variable / results anywhere else, use the local one.

The performance gain by saving the object creation is a) almost non existant and b) most likely offset by the call to clear().

share|improve this answer
add comment

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?

share|improve this answer
    
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. –  petervaz Nov 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. –  Arthur Wulf White Nov 23 '12 at 19:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.