Skip to content

Instantly share code, notes, and snippets.

View dwfait's full-sized avatar

Dwain Faithfull dwfait

View GitHub Profile
require 'benchmark/ips'
class Hash
def old_compact
self.select { |_, value| !value.nil? }
end
def old_compact!
self.reject! { |_, value| value.nil? }
@dwfait
dwfait / output
Last active January 15, 2016 17:31
Calculating -------------------------------------
old 317.000 i/100ms
new 14.732k i/100ms
-------------------------------------------------
old 3.189k (± 3.4%) i/s - 16.167k
new 158.659k (± 3.1%) i/s - 795.528k
Comparison:
new: 158658.9 i/s
old: 3189.4 i/s - 49.75x slower
@dwfait
dwfait / output
Last active January 15, 2016 17:39
Calculating -------------------------------------
old 127.212k i/100ms
new 126.136k i/100ms
-------------------------------------------------
old 3.772M (± 8.8%) i/s - 18.573M
new 3.951M (± 5.8%) i/s - 19.803M
Comparison:
new: 3951356.5 i/s
old: 3771768.5 i/s - 1.05x slower
@dwfait
dwfait / output
Last active January 15, 2016 17:58
Calculating -------------------------------------
== 11.195k i/100ms
eql? 14.323k i/100ms
-------------------------------------------------
== 119.132k (± 4.5%) i/s - 604.530k
eql? 158.007k (± 3.0%) i/s - 802.088k
Comparison:
eql?: 158006.6 i/s
==: 119131.6 i/s - 1.33x slower
@dwfait
dwfait / output
Last active January 18, 2016 18:51
Calculating -------------------------------------
old 124.917k i/100ms
new 127.378k i/100ms
-------------------------------------------------
old 3.766M (± 5.0%) i/s - 18.862M
new 3.797M (± 4.8%) i/s - 18.979M
Comparison:
new: 3797241.3 i/s
old: 3765728.9 i/s - 1.01x slower
Calculating -------------------------------------
old 307.000 i/100ms
new 13.803k i/100ms
-------------------------------------------------
old 3.136k (± 3.7%) i/s - 15.657k
new 155.048k (± 4.0%) i/s - 786.771k
Comparison:
new: 155047.6 i/s
old: 3135.6 i/s - 49.45x slower
o = Object.new
def o.say_hello
puts "Hello!"
end
o.say_hello
o.clone.say_hello
# Benchmark when objects are equivelant:
# frozen_string_literal: true
require 'benchmark/ips'
Benchmark.ips do |x|
a1 = (1..10_000).to_a
a2 = (1..10_000).to_a