Uhm, my game is going 3fps and I think I found..

I benchmarked this segment of code, gave me 3fps.
The reason I did it this way is because I love to organise the items.

Any help on how to achieve my level of organisation without the performance hit?

I should add that I am using buffered images.
Only 2 items are being rendered, background and space ship.


public void render(Graphics2D g)
      {
            ArrayList<Entity> 
                  low = new ArrayList<Entity> (), 
                  med = new ArrayList<Entity> (), 
                  high = new ArrayList<Entity> ();

            Entity hnd = null;
            
            for( Enumeration<Entity> e = items.elements(); e.hasMoreElements(); )
            {
                  hnd = e.nextElement();
                  
                  if( !hnd.isDead() )
                  {
                        if( hnd.getRenderPriority() == PRIORITY_LOW )
                        {
                              low.add(hnd);
                        }
                        else if( hnd.getRenderPriority() == PRIORITY_MEDIUM )
                        {
                              med.add(hnd);
                        }
                        else
                        {
                              high.add(hnd);
                        }
                  }
            }
            
            for( Iterator<Entity> eL = low.iterator(); eL.hasNext(); )
            {
                  eL.next().render(g);
            }
            for( Iterator<Entity> eM = med.iterator(); eM.hasNext(); )
            {
                  eM.next().render(g);
            }
            for( Iterator<Entity> eH = high.iterator(); eH.hasNext(); )
            {
                  eH.next().render(g);
            }
      }

Way too many comparisons in there, and way too much sorting.

Firstly, get rid of all the iterators. Lots of garbage generation and don’t add anything to performance. Stick to a simple indexed getter. You’re running array lists, and you know that, so make use of it. Only ever use iterators if you have a generic List instance and it doesn’t also implement RandomAccess.

Next, all that sorting at the start of the loop is bad. There are many things wrong with that code, starting with the big if/else ladder where you should have used a switch.

However, what you really want to do is throw all that code out and use a priority queue. A single queue will do all your sorting for you before you get to the point of needing to render and you then only need to walk through a single list of values as you render.

Don’t think the code above is the reason why your getting 3fps. Has more to do with how your drawing your images. Maybe you did not understand the benchmark correctly?

cT = System.nanotime();

mainLoop();

dT = System.nanotime() - cT;

fps = (int)1000000000L/dT;

Isn’t that how you would calc fps?

I can tell it’s slow because the input has a very slow response time.

I just use buffered images to render pics.