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

Ok, so clicking on a node essentially bisects its subtree?

It seems to me that most of the program's processing time can be split into 2 categories: sorting the trees, and rendering the nodes. Swing is actually pretty bad at rendering because it's not hardware accelerated, but that doesn't really seem to be the problem here. If you want rendering performance to increase just only render the things that change every frame, don't wipe the screen and re-render everything.

The sort that you're doing is not very efficient:

 //sort the list (bubble sorting)
    for (int i = 0; i < ToBeClustered.size()-1; i++){
        for (int j = 0; j < ToBeClustered.size()-1; j++){
            if (Math.abs(ToBeClustered.get(j).getTargetArea().x - xPointClick) >
                    Math.abs(ToBeClustered.get(j+1).getTargetArea().x - xPointClick)){
               
                ClickableNode TempNode = ToBeClustered.get(j);
                ToBeClustered.set(j,ToBeClustered.get(j+1));
                ToBeClustered.set(j+1,TempNode);
            }
        }
    }

"Bubble sort" is generally known to be a poor sorting algorithm because it's pretty slow for large sets. For example, sorting 100 items with bubble sort would take ~10,000 steps, whereas sorting 100 items with one of the "good" sorting algorithms would take less than 1,000 steps (compare n2 average steps to nlogn average steps). There is a common sorting algorithm called "merge sort" that works by combining two sorted trees into a single larger sorted tree, and I think it might be relevant to your interests. Additionally, since your tree is subdivided (and therefore you're not sorting the entire tree, just different subtrees), you're going to get some efficiency boosts there.

1

u/Illuminatesfolly Mar 21 '13 edited Mar 21 '13

Hey! Thanks for the tip about rendering. Me and the guy that I work with were wondering about that.

AND, good call on the sorting -- I wrote a derivative of quicksort for the custom class that I am working with and now the sort times are trivial. It is actually the portion of the program where I convert clusters to graphical clusters that is eating up time. I put clocks on every portion of the code and found that the portion of the code that does the conversion took something like 10,000X as long as any other portion of the program.

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