Results 1 to 7 of 7
  1. #1
    Neilos is offline Member
    Join Date
    Aug 2011
    Posts
    10
    Rep Power
    0

    Default Performance issue adding and removing lots of objects to Array list, need better way.

    I am making a game using the libgdx library. This information is itself irrelevant except that I am using a game loop rendering as fast as it can all the time.

    In each iteration of the loop I am calculating the distance between all objects and a point, then depending on this distance adding or removing the objects from either 0, 1, 2 or 3 Array Lists. There are up to 1000 objects that I am doing this for each frame The way |I have it set up is to do something like;

    Java Code:
    private void Method (Something something) {
         if (range < 30) {
              // add to list 1
              list1.add(something);
         } else {
              // remove from list 1
              list1.remove(something);
         }
    
         if (range < 20) {
              // add to list 2
              list2.add(something);
         } else {
              // remove from list 2
              list2.remove(something);
         }
    
         if (range < 10) {
              // add to list 2
              list3.add(something);
         } else {
              // remove from list 2
              list3.remove(something);
         }
    }
    This code is killing my performance, can someone show me a better way please?

    Thanks

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Performance issue adding and removing lots of objects to Array list, need better

    Not a lot of info about the use of the something objects.
    Is there a continual creation of new somethings being added to the lists?
    Or is there a fixed number?
    Do the somethings move from one list to another?

    Could you set a flag in the something objects vs removing them from a list.

  3. #3
    Neilos is offline Member
    Join Date
    Aug 2011
    Posts
    10
    Rep Power
    0

    Default Re: Performance issue adding and removing lots of objects to Array list, need better

    Ok well basically the 'something's are game objects created from a class I made called Ship, (basically it is an entity class under a different name), there are lots of different ship classes that extend Ship, for the various ship types.

    Ships are constantly being created and destroyed.

    The ships can exist in more that one list at a time and can exist in a min of two (these two being a list that contains all active ships and a list containing all ships of a certain type, I haven't yet mentioned these two array lists but these aren't the problem as the ships register themselves and remove themselves from these lists very seldom relative to the other three lists already mentioned).

    In my code I wrote in my first post I can see that I am either adding or removing a ship from each of the three lists each frame, with 1000 ships that means 3000 additions or removals each frame, I need to maintain at least 60 fps (180000 additions or removals a second, which are mostly unnecessary as most of the time they are far away ie. more than 30). I do not know how well optimised ArrayLists are for this but it seems to me that this much ArrayList action is unnecessary, I could implement a check if the ship is contained in the list to see if it needs adding but this feels like hammering a square peg in a round hole.

    I am unsure how to go about optimising this code as my current avenue of exploration is not going to work.

    I am happy to drop ArrayLists altogether if there may be a better option, else I am sure that there is a better way to implement my checks so that the CPU isn't hammered so much but I can't think what it would be =(

    If you require more info then please ask.

    Thanks

  4. #4
    Neilos is offline Member
    Join Date
    Aug 2011
    Posts
    10
    Rep Power
    0

    Default Re: Performance issue adding and removing lots of objects to Array list, need better

    Hmmmm I'm just thinking, I cannot see my actual code as it is on another computer but what I have written will keep adding and adding objects to the list even if they are in the list already, meaning that each object in the range specified will get added each frame meaning that my ArrayLists will keep growing and growing with multiple occurrences of the same object, this is gonna suck RAM eventually, I hope that this is not what I am doing as I can see that this would definitely kill my code too. I think that I am actually checking if it is in the list before I add it (well I hope lol).

    Anyway, my original question still stands, disregard the multiple additions of same object to the list (if this is even the case, I'm so confused right now lol).

    Assuming that an object can only exist in a list once, how can I improve the system, are ArrayLists good?

    I suppose that checking if it exists in a list before actually removing it would be less strenuous. Something like this;

    Java Code:
    private void Method (Something something) {
         if (range < 30) {
              if (list1.contains(something)){
                   // do nothing
              } else {
                   // add to list 1
                   list1.add(something);
              }
         } else {
              if (list1.contains(something)) {
                   // remove from list 1
                   list1.remove(something);
              } else {
                   // do nothing
              }
         }
    
    ...
    
    }
    But still I think that a better way will exist, what do you think?

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,183
    Rep Power
    20

    Default Re: Performance issue adding and removing lots of objects to Array list, need better

    Why all the different lists?
    What do they represent?

  6. #6
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default Re: Performance issue adding and removing lots of objects to Array list, need better

    Quote Originally Posted by Neilos View Post
    This code is killing my performance, can someone show me a better way please?Thanks
    perhaps you can improve the performace by using a instance variable of type boolean called destroyed in your shape. instead of removing it from a large list let the shape decide if it's visible or not depending on the variable destroyed.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Performance issue adding and removing lots of objects to Array list, need better

    How about a double linked list. That would be cheaper for adding and removing items

Similar Threads

  1. Accessing objects in array list
    By kev670 in forum New To Java
    Replies: 6
    Last Post: 03-11-2011, 01:49 AM
  2. Removing values from an array in a list.
    By BennyJass in forum AWT / Swing
    Replies: 2
    Last Post: 01-17-2011, 08:00 AM
  3. Need Help in Removing Objects from list
    By pramod_r20 in forum New To Java
    Replies: 2
    Last Post: 12-10-2010, 01:19 PM
  4. Replies: 1
    Last Post: 05-04-2010, 12:00 PM
  5. Replies: 9
    Last Post: 02-10-2010, 04:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •