Created
August 18, 2023 04:54
-
-
Save kenchan/6d84c85b2a86067753a078fa1fd6dc00 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
puts "ruby-#{RUBY_VERSION}" | |
Benchmark.bm(10) do |x| | |
array = (10 ** 5).times.to_a | |
x.report 'sum' do |x| | |
array.sum {|i| i * 2 } | |
end | |
x.report 'each' do |x| | |
ans = 0 | |
array.each {|i| ans += i * 2 } | |
end | |
x.report 'inject' do |x| | |
array.inject(0) {|acc, i| acc + i * 2 } | |
end | |
end | |
__END__ | |
ruby-2.7.1 | |
user system total real | |
sum 0.001611 0.000000 0.001611 ( 0.001606) | |
each 0.001996 0.000000 0.001996 ( 0.001997) | |
inject 0.002512 0.000000 0.002512 ( 0.002512) | |
ruby-3.2.2 | |
user system total real | |
sum 0.001578 0.000526 0.002104 ( 0.002100) | |
each 0.001657 0.000553 0.002210 ( 0.002209) | |
inject 0.002884 0.000000 0.002884 ( 0.002884) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment