Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (489)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
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  
  Array performance question...  (Read 1487 times)
0 Members and 1 Guest are viewing this topic.
kul_th_las
Guest
« Posted 2003-12-21 12:29:37 »

I heard once that multi-dimensional arrays (i.e., 2D array, 3D array, nD array) should be implemented as single-dimension arrays for performance reasons. Is there any validity to this claim?

The array in question for my project is one that is accessed possibly a hundred times or more per frame of my rendering code, so I'll take the performance enhancements where I can get them. Certainly there are larger bottlenecks to fight - is this overkill?
Offline tom
« Reply #1 - Posted 2003-12-21 12:58:08 »

It might be faster because the multi-dimensional arrays would have to do more than one array lookup. And array lookups are slow.

But in your case it's overkill. Don't think you'll ever get a measureable performance benifitt.

Offline elias

Senior Member





« Reply #2 - Posted 2003-12-21 13:15:37 »

As always when in doubt: Implement it in the simplest possible way and profile your way out of bottlenecks later. Many of those so called optimizations mean nothing compared to a few well placed ones found during profiling.

- elias

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

Senior Member





« Reply #3 - Posted 2003-12-21 15:02:04 »

Quote
As always when in doubt: Implement it in the simplest possible way and profile your way out of bottlenecks later. Many of those so called optimizations mean nothing compared to a few well placed ones found during profiling.


This is certainly true for micro-optimizations. At global level, if you program is performance-senstive, IMHO you should think about performance from start - but, to stress it enough, at big level, not for specific micro solutions.

To give example about importance of profiling - real example from my life, happened to me few months ago. One of the method calls on user request was taking 2.5s to complete. This was one of method called from servlet. 2.5s doesn't seem so much for web-based interface, but it was 2.5s of heavy processor usage, so it would kill performance if more than few clients would call it at same time.

A LOT of reflection was used inside this method. This was my first 'sure' candidate for optimalization, as same Methods was created again and again for same calls. I implemented classloader/class aware cache and measured speed again... 1900ms. On one hand, 600ms is not a bad gain, but what is taking rest of time ?? I have run simple profiler and found the offender... inside most inner loop, log was called (leftover from testing phase). It was constructing string with info and logging it - writing multiple megabytes to synchronized log line after line... After removing this line, method now takes 20-30ms, which is on the same order of magnitude as measurement error.

Of course, if I would not have implemented cache, it would take 500-600ms, which is still too long. But without profiling, reflection itself was main suspect for being slow and we have already considered rewriting everything from scratch without reflection (a MAJOR undertaking).

Back to your problem. There are few problems with multi-dimension arrays in java. Jits cannot optimize bound checks very well, you pay array space overhead each time and you are not cache friendly. Extra dereference comes somewhere at the end of scale.

How important are these structures ? For example, if you want to keep your vertex list in array, float[vertices][3] is out of question. If you just want to represent some data which is loaded from/sent to net, then structure is probably not important, as net access will be only thing affecting speed of program.

My solution ? Abstract access to these arrays. Create a wrapper class representing your data (thing like VertexList or Matrix) and then you can not care about actual implementation since very last stage of project. Current jvms inline all calls almost perfectly, so you will have same performance as without wrapper.

Artur Biesiadowski
Offline Jeff

JGO Coder




Got any cats?


« Reply #4 - Posted 2003-12-23 16:39:47 »

All the suggestions here are good. Another reason why multi-dimensional arrays COULD be slower is that you have multiple indexes to bounds-check.  This matters more if your access is non-squential (not in loops), much less if it is sequential.

The best advice here, IMO, though is right above this post. NEVER design data structure assumptions into your code.  Design first with interfaces to define functionality.  This will keep your implementations un-intertwined and thus allow for maximal tunability.

In "Effective Java" Josh Bloch recommends that you not write a single class file until all of your interfaces are written and compiling against each other.  Its a very good exercise to do a few times.  Once you've done it that way a few times you can relax it a bit because you will still have trained yourself to "think interfaces."

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
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.

TehJavaDev (18 views)
2014-08-28 18:26:30

CopyableCougar4 (26 views)
2014-08-22 19:31:30

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

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

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

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

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

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

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

BurntPizza (37 views)
2014-08-08 02:01:56
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!