I just wanted to note the differences in behavior and performance for some solutions here. Behavior "communication failure" duplicates :
a = [3,1,2,3] a.each_with_index.max[1]
Out of curiosity, I spent them at Benchmark.bm (for a above):
user system total real each_with_index.max 0.000000 0.000000 0.000000 ( 0.000011) index.max 0.000000 0.000000 0.000000 ( 0.000003)
Then I generated a new a with Array.new(10_000_000) { Random.rand } and repeated the test:
user system total real each_with_index.max 2.790000 0.000000 2.790000 ( 2.792399) index.max 0.470000 0.000000 0.470000 ( 0.467348)
This makes me think if you don't need to select a higher index max, a.index(a.max) is the best choice.
Alex Moore-Niemi Dec 14 '17 at 1:44 2017-12-14 01:44
source share