Skip to content

Instantly share code, notes, and snippets.

@jkthorne
Created May 1, 2019 18:20
Show Gist options
  • Save jkthorne/563d491cb717c0fd645d319ddd73a702 to your computer and use it in GitHub Desktop.
Save jkthorne/563d491cb717c0fd645d319ddd73a702 to your computer and use it in GitHub Desktop.
require "benchmark"
module Enumerable
def j8r_join(separator, io)
first = true
each do |elem|
io << separator unless first
yield elem, io
first = false
end
end
def new_join(separator, io)
first = true
each do |elem|
io << separator unless first
yield elem, io
first = false if first
end
end
end
SINGLE = Array.new(9, 123)
DOUBLE = Array.new(99, 123)
TRIPLE = Array.new(999, 123)
QUADRUPLE = Array.new(9999, 123)
QUINTUPLE = Array.new(99999, 123)
SEXTUPLE = Array.new(999999, 123)
SEPTUPLE = Array.new(9999999, 123)
# SINGLE = StaticArray(Int32, 9).new 123
# DOUBLE = StaticArray(Int32, 99).new 123
# TRIPLE = StaticArray(Int32, 999).new 123
# QUADRUPLE = StaticArray(Int32, 9999).new 123
# QUINTUPLE = StaticArray(Int32, 99999).new 123
# SEXTUPLE = StaticArray(Int32, 999999).new 123
# SEPTUPLE = StaticArray(Int32, 9999999).new 123
Benchmark.ips(0, 2) do |x|
x.report("single j8r join") { SINGLE.j8r_join("", STDOUT) {|e|} }
x.report("single new join") { SINGLE.new_join("", STDOUT) {|e|} }
x.report("double j8r join") { SINGLE.j8r_join("", STDOUT) {|e|} }
x.report("double new join") { SINGLE.new_join("", STDOUT) {|e|} }
x.report("triple j8r join") { TRIPLE.j8r_join("", STDOUT) {|e|} }
x.report("triple new join") { TRIPLE.new_join("", STDOUT) {|e|} }
x.report("quadruple new join") { QUADRUPLE.new_join("", STDOUT) {|e|} }
x.report("quadruple j8r join") { QUADRUPLE.j8r_join("", STDOUT) {|e|} }
x.report("quintuple j8r join") { QUINTUPLE.j8r_join("", STDOUT) {|e|} }
x.report("quintuple new join") { QUINTUPLE.new_join("", STDOUT) {|e|} }
x.report("sextuple j8r join") { SEXTUPLE.j8r_join("", STDOUT) {|e|} }
x.report("sextuple new join") { SEXTUPLE.new_join("", STDOUT) {|e|} }
x.report("septuple j8r join") { SEPTUPLE.j8r_join("", STDOUT) {|e|} }
x.report("septuple new join") { SEPTUPLE.new_join("", STDOUT) {|e|} }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment