Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (769)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (856)
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  
  for loops  (Read 2409 times)
0 Members and 1 Guest are viewing this topic.
Offline VoidBuffer

Junior Devvie


Medals: 4
Exp: 2 years



« Posted 2017-11-09 21:28:50 »

Does anyone have an argument for which for loops they prefer for efficiency reasons? I've read arguments against using iterators because they are an object that needs to be garbage collected, versus a traditional for loop.
Offline FabulousFellini
« Reply #1 - Posted 2017-11-09 22:05:30 »

I use regular for loops on my game and foreach at work, because that's their standard.

-FabulousFellini
www.fabulousfellini.com
Offline tariqbroadnax

Senior Devvie


Medals: 7
Exp: 3 years



« Reply #2 - Posted 2017-11-10 03:41:05 »

You must have a fully developed game if you have time to worry about for loops. #programmerproblems

I personally use them how java begs you too. Foreach for collections. For for when I need the index.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline VoidBuffer

Junior Devvie


Medals: 4
Exp: 2 years



« Reply #3 - Posted 2017-11-10 04:35:40 »

GC is an issue that I think we should all be paying attention too, so coding with that in mind helps code in the future IMO. Just wondering on your thoughts Smiley
Offline KevinWorkman

« JGO Plugged Duke »


Medals: 285
Projects: 12
Exp: 12 years


HappyCoding.io - Coding Tutorials!


« Reply #4 - Posted 2017-11-10 05:38:52 »

Just use whichever one makes more sense to you in your context.

Also, you really shouldn't be worrying about "efficiency" until you've actually measured your performance.

See also: https://stackoverflow.com/questions/385506/when-is-optimisation-premature

HappyCoding.io - Coding Tutorials!
Happy Coding forum - Come say hello!
Offline VoidBuffer

Junior Devvie


Medals: 4
Exp: 2 years



« Reply #5 - Posted 2017-11-10 06:07:26 »

Yes I agree. I'm at the stage of making micro optimizations. Thought it would be an interesting discussion.
Offline h.pernpeintner

JGO Ninja


Medals: 100



« Reply #6 - Posted 2017-11-10 09:33:33 »

Yes it makes a difference. At least if you know the implementation of the list you want to iterate, you can chose the more efficient way. Take a look at the O notation talked about here https://stackoverflow.com/questions/2113216/which-is-more-efficient-a-for-each-loop-or-an-iterator

Additionally, the magic iterator object is somehow source of mystery. Normally, the iterator object should be created on the stack by the runtime, hence it's gone after the return of the method and no GC involved. However, I (and many others) were able to take measurements where one could clearly see, that there is actually a lot of garbage created for this iterator....BUT: It's possible that the circumstands of benchmarking and profiling just cause that the runtime doesn't use stack allocation here. You can find several discussion over that on the internet, and I think even in this forum. That was the point where I gave up and switched to c loops for everything running in the production build of "my game". Normally, I would say that one shouldn't care about it, since the iterator is a very small, short-living object where the runtime is optimized for. But in a gameloop, running potentially up to douzens of thousands of times per second, the amount of garbage could be gigantic. So yes....premature optimization is the root of all evil, we all know Smiley
Offline princec

« JGO Spiffy Duke »


Medals: 1059
Projects: 3
Exp: 20 years


Eh? Who? What? ... Me?


« Reply #7 - Posted 2017-11-10 09:55:21 »

Or for short, attempting to profile it drastically changes the reality. As Kevin says use the one that looks nicest and simplest, and don't trust what the profiler says.

Cas Smiley

Offline nsigma
« Reply #8 - Posted 2017-11-10 09:56:46 »

There should be a good possibility the iterator is removed by escape analysis and so not be a GC concern.  Premature optimization is bad, relying on profiling to tell you what's happening can be worse - I refer you to this message from earlier this year where @spasi talks about this exact issue (and recommends JITWatch)

Of course, there's another option with collections these days -

1  
2  
3  
list.forEach(item -> {
  // do something with item
});


That uses an internal for loop.  Internal iteration has the potential to be faster than anything else.

Praxis LIVE - hybrid visual IDE for (live) creative coding
Offline philfrei
« Reply #9 - Posted 2017-11-10 19:23:40 »

Question: didn't @Riven once publish a structure that optimized for contiguous memory for objects? "MappedObjects"? Was there a preferred iteration process involved? Is that approach still considered useful? At the time it came out, it was a bit over my skill set, and I wasn't able to learn the lessons it provided.

music and music apps: http://adonax.com
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ddyer
« Reply #10 - Posted 2017-11-11 21:00:56 »

I traditionally used For loops, but I recently came up with a good reason to prefer a custom iterator model.

The rendering style I use depends on drawing order to look right, given potentially overlapping
objects.  For most boards, that is top to bottom, left to right.  However, it you offer the "reverse"
view, you also have to reverse drawing order, which complicates the clean "for" loop a bit.  Now
add the "rotate 90 degrees" and you also have to swap the x and y axis, which REALLY complicates
things.  You can't avoid all the details everywhere, but a "for loop" model gets very messy. Using
iterators leaves the apparent code very clean and hides the messy details in the guts of the iterator,
which you only have to write once.

Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (1672 views)
2018-06-10 19:43:48

EgonOlsen (1718 views)
2018-06-10 19:43:44

EgonOlsen (1159 views)
2018-06-10 19:43:20

DesertCoockie (1585 views)
2018-05-13 18:23:11

nelsongames (1186 views)
2018-04-24 18:15:36

nelsongames (1713 views)
2018-04-24 18:14:32

ivj94 (2539 views)
2018-03-24 14:47:39

ivj94 (1765 views)
2018-03-24 14:46:31

ivj94 (2844 views)
2018-03-24 14:43:53

Solater (971 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
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!