Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
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 486 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.
Online 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

Junior Devvie


Medals: 2
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: 270
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 Knight


Medals: 62



« 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: 973
Projects: 3
Exp: 16 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  
 
 

 
Ecumene (107 views)
2017-09-30 02:57:34

theagentd (134 views)
2017-09-26 18:23:31

cybrmynd (244 views)
2017-08-02 12:28:51

cybrmynd (236 views)
2017-08-02 12:19:43

cybrmynd (237 views)
2017-08-02 12:18:09

Sralse (251 views)
2017-07-25 17:13:48

Archive (863 views)
2017-04-27 17:45:51

buddyBro (1006 views)
2017-04-05 03:38:00

CopyableCougar4 (1564 views)
2017-03-24 15:39:42

theagentd (1372 views)
2017-03-24 15:32:08
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
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!