Skip to content

Instantly share code, notes, and snippets.

@gr33n7007h

gr33n7007h/adam Secret

Created Oct 18, 2019
Embed
What would you like to do?
benchmark
☺️ /home/gr33n7007h $ cat adam.rb
require 'benchmark/ips'
data = 100_000.times.map { |i| "#{i} #{[*?a..?z].sample(7).join}\n" }.join
Benchmark.ips do |x|
x.report("lines") { data.lines.map{ |line| line.split }.map{ |pid, prog| [pid.to_i, prog] } }
x.report("each_slice") { data.split.each_slice(2).map { |pid, prog| [pid.to_i, prog] } }
x.report("with_object") { data.split.each_slice(2).with_object([]) { |(pid, prog), container| container << [pid.to_i, prog] } }
x.report("each_line") { data.each_line.map { |line| line.split.yield_self {|pid, prog| [pid.to_i, prog] } } }
x.compare!
end
☺️ /home/gr33n7007h $ ruby adam.rb
Warming up --------------------------------------
lines 1.000 i/100ms
each_slice 2.000 i/100ms
with_object 1.000 i/100ms
each_line 1.000 i/100ms
Calculating -------------------------------------
lines 13.661 (±14.6%) i/s - 66.000 in 5.032011s
each_slice 19.199 (± 5.2%) i/s - 96.000 in 5.008917s
with_object 13.444 (± 7.4%) i/s - 67.000 in 5.019100s
each_line 11.320 (± 8.8%) i/s - 56.000 in 5.000041s
Comparison:
each_slice: 19.2 i/s
lines: 13.7 i/s - 1.41x slower
with_object: 13.4 i/s - 1.43x slower
each_line: 11.3 i/s - 1.70x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.