r/CircleProgramming • • Mar 21 '13

How do I make this code faster without loosing the function?

http://pastebin.com/ie9XTrc7
3 Upvotes

11 comments sorted by

View all comments

Show parent comment

2

u/cokeisahelluvadrug Mar 21 '13

That's awesome, good to hear

1

u/Illuminatesfolly Mar 21 '13

Is there any way that I can force Swing to render more quickly? I don't think there would be without that solution being "use something other than swing", but maybe...?

2

u/cokeisahelluvadrug Mar 22 '13 edited Mar 22 '13

Yeah, when I said that Swing doesn't use hardware acceleration that wasn't totally accurate, I really meant that it doesn't use hardware acceleration "by default".

I'm not really experienced in this, but I did a quick Google search and it looks like the big performance difference between Java AWT ("Java 2D") and Java Swing is that Swing uses a technique called "double buffering", which offers a huge performance boost. Basically how this works is instead of redrawing everything every frame, it just keeps track of what changes between frames and uses some graphics techniques to minimize overdraw.

Here's a rundown of hardware acceleration in Java that I found:

Hardware acceleration: HA is a tricky subject with Java. Realistically, whether or not you get it is at the discretion of the JVM and the system you're using. To my knowledge, the only reliable way to accelerate a drawing context without using VolatileImage directly in Java2D is to use full-screen exclusive mode and remains the only way to do so on older systems, since FSEM reserves the drawing context for your application only. Newer systems shouldn' t have that issue but it still remains a difficult thing to guarantee on an end user.

So, basically you as the programmer don't have much control over how the actual rendering is accelerated. If you are using Java AWT you can modify something called java.awt.image.VolatileImage, but again Java.Swing bypasses this issue entirely.

edit: I didn't really answer your point. There are a few things you can do, but they are mostly common sense. Make sure you aren't rendering things multiple times, and don't render things if they're not visible. Make sure that your resource files are at the resolution you need, don't use a 2000x2000 png if all you need is a 20x20 bmp.

1

u/Illuminatesfolly Mar 22 '13

Actually, very informative, and you answered the point that I meant to make, but worded incorrectly the first time. The common sense stuff in rendering is done --

changing the for loop syntax in the problematic section of code

from

for (int i; i < #, i++)

to

for (datatype : datatype list)

accelerated the process by 3x (from around 30 seconds to 10 seconds) -- which is acceptable.

1

u/cokeisahelluvadrug Mar 22 '13

That's interesting. I assume by "#" you're calling list.size() or something similar. If you're not aware, what you're actually doing is swapping out usage of "index" values for "iterators" -- iterators are little objects that let you iterate through the elements of a container, and all good containers in Java use them.

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Iterator.html

for (datatype currentNode : datatype list)

is actually equivalent to:

for(Iterator<datatype> iter = list.iterator() ; iter.hasNext(); ) {
     datatype currentNode = iter.next();
}

1

u/Illuminatesfolly Mar 22 '13 edited Mar 22 '13

Oh that's cool. I know that elsewhere in our code, we actually use the iterator notation to go through lists, but it is usually so cumbersome (in comparison to a foor loop) that we prefer the latter.

However, it makes a lot of sense when considering the small differences between iteration time and for loop time stacked at 100 times or so.

2

u/cokeisahelluvadrug Mar 22 '13

Yeah, especially when you need to do everything in less than 1/30th of a second