Created
June 19, 2018 08:23
-
-
Save janzikan/c780775267dd5483a8271f7e5afe15cd to your computer and use it in GitHub Desktop.
Ruby: sum vs inject benchmark
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
Ruby version 2.4.1 | |
Sum of 100 numbers | |
Warming up -------------------------------------- | |
sum 190.410k i/100ms | |
inject 184.380k i/100ms | |
Calculating ------------------------------------- | |
sum 3.303M (± 1.5%) i/s - 33.131M in 10.034500s | |
inject 3.046M (± 1.7%) i/s - 30.607M in 10.050375s | |
Comparison: | |
sum: 3302547.9 i/s | |
inject: 3046295.6 i/s - 1.08x slower | |
--- | |
Sum of 1 000 numbers | |
Warming up -------------------------------------- | |
sum 51.790k i/100ms | |
inject 55.931k i/100ms | |
Calculating ------------------------------------- | |
sum 583.830k (± 0.9%) i/s - 5.852M in 10.024782s | |
inject 638.967k (± 2.2%) i/s - 6.432M in 10.071645s | |
Comparison: | |
inject: 638966.7 i/s | |
sum: 583829.9 i/s - 1.09x slower | |
--- | |
Sum of 10 000 numbers | |
Warming up -------------------------------------- | |
sum 6.183k i/100ms | |
inject 7.056k i/100ms | |
Calculating ------------------------------------- | |
sum 63.090k (± 0.8%) i/s - 636.849k in 10.094933s | |
inject 71.878k (± 0.9%) i/s - 719.712k in 10.013696s | |
Comparison: | |
inject: 71878.4 i/s | |
sum: 63090.0 i/s - 1.14x slower | |
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/ips' | |
NUMBERS = (1..10_000).to_a | |
Benchmark.ips do |x| | |
x.config(time: 10, warmup: 2) | |
x.report('sum') do | |
NUMBERS.sum | |
end | |
x.report('inject') do | |
NUMBERS.inject(:+) | |
end | |
x.compare! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment