From c956e17c02d890ed96f640504f57eae0495f1f75 Mon Sep 17 00:00:00 2001 From: Christopher Aue Date: Sat, 26 Aug 2017 15:42:52 +0200 Subject: Replaced Array#each with while loop for performance reasons Example benchmark: $ time build/bench/bin/mruby -e "Array.new(2_000_000){ |i| i }.index{ |i| i == 1_999_999 }" Before: real 0m0.934s user 0m0.922s sys 0m0.003s After: real 0m0.590s user 0m0.583s sys 0m0.007s --- mrblib/array.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'mrblib') diff --git a/mrblib/array.rb b/mrblib/array.rb index 5b9ee47c0..a75ed6223 100644 --- a/mrblib/array.rb +++ b/mrblib/array.rb @@ -46,7 +46,12 @@ class Array def collect!(&block) return to_enum :collect! unless block - self.each_index { |idx| self[idx] = block.call(self[idx]) } + idx = 0 + len = size + while idx < len + self[idx] = block.call self[idx] + idx += 1 + end self end -- cgit v1.2.3