summaryrefslogtreecommitdiffhomepage
path: root/mrblib
diff options
context:
space:
mode:
authorChristopher Aue <[email protected]>2017-08-26 15:42:52 +0200
committerChristopher Aue <[email protected]>2017-08-26 15:57:25 +0200
commitc956e17c02d890ed96f640504f57eae0495f1f75 (patch)
tree1874af71ff598308afbd8f1110bd5ebdcf6f445c /mrblib
parent6c9013e22b56f8a6b9bbd8ba2defa9db6a768401 (diff)
downloadmruby-c956e17c02d890ed96f640504f57eae0495f1f75.tar.gz
mruby-c956e17c02d890ed96f640504f57eae0495f1f75.zip
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
Diffstat (limited to 'mrblib')
-rw-r--r--mrblib/array.rb7
1 files changed, 6 insertions, 1 deletions
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