Skip to content

Instantly share code, notes, and snippets.

@texpert
Last active June 17, 2022 01:14
Show Gist options
  • Save texpert/ba25f664562a831a238f44c106a67fdf to your computer and use it in GitHub Desktop.
Save texpert/ba25f664562a831a238f44c106a67fdf to your computer and use it in GitHub Desktop.
Code:
-----------------------------
# frozen_string_literal: true
# Install the necessary gems:
# gem install benchmark-ips
# gem install kalibera
require 'benchmark/ips'
# Enable and start GC before each job run. Disable GC afterwards.
#
# Inspired by https://www.omniref.com/ruby/2.2.1/symbols/Benchmark/bm?#annotation=4095926&line=182
class GCSuite
def warming(*)
run_gc
end
def running(*)
run_gc
end
def warmup_stats(*)
end
def add_report(*)
end
private
def run_gc
GC.enable
GC.start
GC.disable
end
end
suite = GCSuite.new
Benchmark.ips do |x|
x.config(:time => 10, :warmup => 5, :stats => :bootstrap, :confidence => 95)
x.report('Array#unshift') do
array = []
100_000.times { |i| array.unshift(i) }
end
x.report('Array#shift') do
array = []
100_000.times { |i| array.shift(i) }
end
x.report('Array#insert') do
array = []
100_000.times { |i| array.insert(0, i) }
end
x.report('Array#<<') do
array = []
100_000.times { |i| array << i }
end
x.compare!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment