require 'benchmark'
$__benchmarks__ = []
def bench(name, iterations = 1000, &block)
file, line, _ = caller[0].split(':')
$__benchmarks__ << {
file: File.basename(file),
line: line,
name: name,
iterations: iterations,
block: block
}
end
at_exit do
Benchmark.bmbm do |x|
$__benchmarks__.each do |info|
benchname = "#{info[:file]}:#{info[:line]} #{info[:name]}"
x.report(benchname) { info[:iterations].times(&info[:block]) }
end
end
end
other_array = [*1..1000]
bench ".each" do
array = [*1..1000]
other_array.each do |elem|
array << elem
end
end
bench "#concat" do
array = [*1..1000]
array.concat(other_array)
end
cmVxdWlyZSAnYmVuY2htYXJrJwoKJF9fYmVuY2htYXJrc19fID0gW10KCmRlZiBiZW5jaChuYW1lLCBpdGVyYXRpb25zID0gMTAwMCwgJmJsb2NrKQogIGZpbGUsIGxpbmUsIF8gPSBjYWxsZXJbMF0uc3BsaXQoJzonKQogICRfX2JlbmNobWFya3NfXyA8PCB7CiAgICBmaWxlOiBGaWxlLmJhc2VuYW1lKGZpbGUpLAogICAgbGluZTogbGluZSwKICAgIG5hbWU6IG5hbWUsCiAgICBpdGVyYXRpb25zOiBpdGVyYXRpb25zLAogICAgYmxvY2s6IGJsb2NrCiAgfQplbmQKCmF0X2V4aXQgZG8KICBCZW5jaG1hcmsuYm1ibSBkbyB8eHwKICAgICRfX2JlbmNobWFya3NfXy5lYWNoIGRvIHxpbmZvfAogICAgICBiZW5jaG5hbWUgPSAiI3tpbmZvWzpmaWxlXX06I3tpbmZvWzpsaW5lXX0gI3tpbmZvWzpuYW1lXX0iCiAgICAgIHgucmVwb3J0KGJlbmNobmFtZSkgeyBpbmZvWzppdGVyYXRpb25zXS50aW1lcygmaW5mb1s6YmxvY2tdKSB9CiAgICBlbmQKICBlbmQKZW5kCgpvdGhlcl9hcnJheSA9IFsqMS4uMTAwMF0KCmJlbmNoICIuZWFjaCIgZG8KICBhcnJheSA9IFsqMS4uMTAwMF0KICBvdGhlcl9hcnJheS5lYWNoIGRvIHxlbGVtfAogICAgYXJyYXkgPDwgZWxlbQogIGVuZAplbmQKCmJlbmNoICIjY29uY2F0IiBkbwogIGFycmF5ID0gWyoxLi4xMDAwXQogIGFycmF5LmNvbmNhdChvdGhlcl9hcnJheSkKZW5kCg==