haxpor
07/14/2017, 1:54 AMfilter{}
, map{}
over normal loop code. Yes, I like to use those. But peek into the code from IDE reveals that actually filter{}
is not better than normal loop as it dynamically creates a new ArrayList
object to hold filtered elements. So normal loop would slightly has better performance.
My question is as IDE suggests me like this
- Is the performance that significant (although I can’t feel any difference)?
- Is there anyone already did performance test comparing between filter{}
, map{}
etc against normal way?
- Should I always use lambda functions as also suggested by IDE? (I want to but from above, I didn’t feel safe)marstran
07/14/2017, 7:12 AMhaxpor
07/14/2017, 7:23 AMforEach
as I peeked in. Any idea, or suggestion?marstran
07/14/2017, 7:28 AMflyingSaucers.filter { !it.shouldBeRemoved }
.flatMap { it.bullets }
.filter { !it.shouldBeRemoved && player.contains(it.x, it.y) }
.forEach {
it.shouldBeRemoved = true
player.hit()
Game.res.getSound("explode")?.play()
}
Game.res.getSound("explode")
outside the loop though.haxpor
07/14/2017, 7:32 AMmarstran
07/14/2017, 7:32 AMhaxpor
07/14/2017, 7:32 AMflatMap
when possible.marstran
07/14/2017, 7:37 AM.asSequence()
to make it lazy though, but I'm not really sure if it would make a difference here.haxpor
07/14/2017, 7:39 AMfilter
will require to create ArrayList to hold result, but asSequence()
is better too I think.marstran
07/14/2017, 7:53 AMhaxpor
07/15/2017, 8:34 AMtakeIf
or takeUnless
, so I can combine forEach
with takeIf
without creating new object in filter
before going into the loop.