projectmoon
06/07/2019, 4:04 PM5.times |x| mylist << x
in rubypavi2410
06/07/2019, 7:03 PM(1..5).forEach { num -> }
?nkiesel
06/17/2019, 6:31 PMList<Int>(5) { it + 1 }
(or it
, not sure if that Ruby code starts with 0
or 1
pavi2410
06/17/2019, 6:59 PMMike
06/17/2019, 7:05 PMCasey Brooks
06/17/2019, 7:10 PM(1..5).forEach { }
does not keep the entire list in-memory, it is just an iterator, so larger ranges do not take up more memory. List<Int>(5) { }
holds all elements in memory, and thus cannot be used for really large rangesnkiesel
06/17/2019, 9:00 PMnanoTime
for micro-benchmarks like this. Not because of the granularity but because that really measures the time the JVM was running instead of elapsed system time. But more to the point, the 2 do something very different: the repeat
calls a block n
times and throws away the returned values while the List(n) { block }
keeps all the values. So why are we comparing the 2? I could see to compare forEach
with repeat
or a for
loop.pavi2410
06/18/2019, 6:18 PM