-
-
Save omockler/1add869640cb8010a7f0 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/ips' | |
def group | |
(0..99).group_by { |i| i % 2 } | |
end | |
def push | |
even = [] | |
odd = [] | |
(0..99).each do |i| | |
if i % 2 == 0 | |
even << i | |
else | |
odd << i | |
end | |
end | |
end | |
def partition | |
even, odd = (0..99).partition { |i| i % 2 == 0 } | |
end | |
def select | |
(0..99).select { |i| i % 2 != 0 } | |
end | |
def single_push | |
even = [] | |
(0..99).each do |i| | |
if i % 2 == 0 | |
even << i | |
end | |
end | |
end | |
Benchmark.ips do |x| | |
x.report("group") { |times| times.times do group; end } | |
x.report("push") { |times| times.times do push; end } | |
x.report("partition") { |times| times.times do partition; end } | |
x.report("select") { |times| times.times do select; end } | |
x.report("single push") { |times| times.times do single_push; end } | |
x.compare! | |
end |
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
Calculating ------------------------------------- | |
group 4.576k i/100ms | |
push 5.732k i/100ms | |
partition 5.693k i/100ms | |
select 6.382k i/100ms | |
single push 7.414k i/100ms | |
------------------------------------------------- | |
group 60.612k (±13.4%) i/s - 297.440k | |
push 76.997k (±10.6%) i/s - 384.044k | |
partition 73.989k (± 6.6%) i/s - 370.045k | |
select 80.999k (± 6.1%) i/s - 408.448k | |
single push 102.651k (± 6.6%) i/s - 511.566k | |
Comparison: | |
single push: 102650.5 i/s | |
select: 80999.4 i/s - 1.27x slower | |
push: 76996.7 i/s - 1.33x slower | |
partition: 73989.1 i/s - 1.39x slower | |
group: 60611.6 i/s - 1.69x 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
Calculating ------------------------------------- | |
group 4.576k i/100ms | |
push 5.732k i/100ms | |
partition 5.693k i/100ms | |
select 6.382k i/100ms | |
single push 7.414k i/100ms | |
------------------------------------------------- | |
group 60.612k (±13.4%) i/s - 297.440k | |
push 76.997k (±10.6%) i/s - 384.044k | |
partition 73.989k (± 6.6%) i/s - 370.045k | |
select 80.999k (± 6.1%) i/s - 408.448k | |
single push 102.651k (± 6.6%) i/s - 511.566k | |
Comparison: | |
single push: 102650.5 i/s | |
select: 80999.4 i/s - 1.27x slower | |
push: 76996.7 i/s - 1.33x slower | |
partition: 73989.1 i/s - 1.39x slower | |
group: 60611.6 i/s - 1.69x slower |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment