Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (546)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  ArrayLists and Receding Hairline  (Read 2986 times)
0 Members and 1 Guest are viewing this topic.
Offline AwexGreg

Junior Newbie





« Posted 2009-10-01 21:02:59 »

Hi again,

I have built a (theoretically) wonderful gfx framework for my upcoming games to make life easier, that was the purpose anyway. I'll explain what it's all about.
I read a book on ActionScript 3 the other day and there I found a rather good solution for chaining Sprites together, they call it DisplayList in AS. The whole idea is that you've got the "stage" where each and every one of the DisplayObjects belong. Then you've got DisplayObjectContainers that can hold other DisplayObjectContainers or DisplayObjects. It's very easy to add or remove sprites to/ from this displayList, swap objects, etc. I copied this idea and I built my own little DislpayList in Java. Works perfectly, it is very comfortable to use, I am a hundred percent satisified with it apart from one thing: it's f**kin slow.

The problem is ArrayList: I'm using it to keep references of children objects. So every time the program attempts to repaint what's on the displaylist it has to iterate through all the members which process proved to be a lot slower than what I expected. (I tested it with and without arrayLists, if I use arraylists with only 2-3 objects added to the list I lose 8-10 frames per second.. and there are meant to be DOZENS of objects in the displayList, not 2-3!!! )
I KNOW how much quicker simple arrays are but they've got a fixed size which makes them absolutely useless in this case. I need a flexible solution but a lot quicker than this stupid arraylist.


Any solution?
Or did anyone understand this gibberish post at all?


Well, any answers are welcome
Greg
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 781
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2009-10-01 21:59:03 »

ArrayLists are only marginally (probably max factor 2) slower
than plain old arrays. Only removing from an ArrayList is slow,
but it would be slow on an array too, as you have to shift a lot
of items 'to the left'. Are you sure you're barking up the right tree?

Did you profile your app to see where the bottleneck is?

How are you rendering? Java2D, OpenGL? Maybe you did
something that disabled hardware acceleration? Show us
some code!

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline AwexGreg

Junior Newbie





« Reply #2 - Posted 2009-10-02 00:56:50 »

Hmm you made a point here Riven, I actually didn't profile it, I might do it in the next 2 minutes .. However due to my calculations using ArrayList appears to be the root of the problem. If I replace ArrayList with Arrays my program produces almost 20% better performance. I don't use JOGL, I'm working with plain old J2D. Even 2D is quite confusing to me but hey that's the thing about java, it's meant to be confusing to keep the bloody "flashers" away, innit Smiley) No hardware acceleration so far, but I'm planning to keep sprites in VolatileImages. Right now the program keeps everything in BufferedImages. I also use double buffering, which is a good thing apparently.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #3 - Posted 2009-10-02 03:22:53 »

Java2D is way slow if hardware acceleration is not happening. In almost all Java2D games I've made, the bottleneck is in the rendering, not the logic. See how fast it goes if you turn off drawing.

See my work:
OTC Software
Offline zammbi

JGO Coder


Medals: 4



« Reply #4 - Posted 2009-10-02 06:27:39 »

If you think arraylist is your trouble then you can use another collection class. I used a few HashMaps in one of my games for caching objects.
But make sure you understand when to use a collection over another.

Current project - Rename and Sort
Offline JL235

JGO Coder


Medals: 10



« Reply #5 - Posted 2009-10-02 07:16:08 »

Are you doing any searching in these ArrayLists? That is the only expense I can see with using them ArrayLists. How are you measuring the performance? Did you profile it?

I also agree that (typically) the real expense should be the graphics code, not the logic. If your back end lists are that slow then your doing something silly with them.

I also use a very similar system for my games, works perfectly.

Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #6 - Posted 2009-10-02 08:53:46 »

If switching from ArrayLists to arrays really gives you that much of a speed increase then something is very wrong or you're doing something daft. Post code so we can actually see wft you're doing.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline i30817

Junior Member





« Reply #7 - Posted 2009-10-02 11:56:26 »

Probably removing from the list.
Offline Mr. Gol

Senior Member


Medals: 1



« Reply #8 - Posted 2009-10-02 12:25:00 »

If jMonkeyEngine can implement their scene graph using lists of nodes (see http://code.google.com/p/jmonkeyengine/source/browse/trunk/src/com/jme/scene/Node.java), and have 1000s of nodes in the scene with a good framerate, I'm quite sure ArrayList isn't the problem Smiley
Offline AwexGreg

Junior Newbie





« Reply #9 - Posted 2009-10-02 15:22:37 »

Ok, ok, got the message guys. Since every one of you say the problem can't be the ArrayList I believe you and try to dig deeper. It's probably the rendering that does something dodgy.


Regardless, I'm still losing hair.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline JL235

JGO Coder


Medals: 10



« Reply #10 - Posted 2009-10-02 16:31:36 »

Probably removing from the list.
Second this, certain operations are very slow on array lists and it could be that your just setting it to null in an array (and so avoiding the reordering that an arraylist performs).

Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #11 - Posted 2009-10-02 16:44:02 »

Alternatively, a long shot would be over-use of enhanced-for-loop usage creating too much iterator churn and stressing the gc. Although I think you'd have to be doing silly amounts of iteration for that to be a problem these days.

Post code!

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #12 - Posted 2009-10-03 18:18:13 »

Ok, ok, got the message guys. Since every one of you say the problem can't be the ArrayList I believe you and try to dig deeper. It's probably the rendering that does something dodgy.


Regardless, I'm still losing hair.
Just try turning various things off (especially rendering) and then see what makes it run faster. Simple and easy, no? Or, just profile your code a bit with nanoTime. Either way, actually find out what's causing the problem, it's not too difficult of a task, it's just time consuming.

See my work:
OTC Software
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

atombrot (20 views)
2014-08-19 09:29:53

Tekkerue (21 views)
2014-08-16 06:45:27

Tekkerue (21 views)
2014-08-16 06:22:17

Tekkerue (12 views)
2014-08-16 06:20:21

Tekkerue (19 views)
2014-08-16 06:12:11

Rayexar (55 views)
2014-08-11 02:49:23

BurntPizza (37 views)
2014-08-09 21:09:32

BurntPizza (27 views)
2014-08-08 02:01:56

Norakomi (35 views)
2014-08-06 19:49:38

BurntPizza (64 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!