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

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