Skip to content

Instantly share code, notes, and snippets.

@PJK
Created February 1, 2015 17:26
Show Gist options
  • Save PJK/cdd4de5092eb828ea002 to your computer and use it in GitHub Desktop.
Save PJK/cdd4de5092eb828ea002 to your computer and use it in GitHub Desktop.
contracts
require 'rubygems'
require 'bundler'
Bundler.require
include Contracts
Contract Num, Num => Num
def add_c(a, b)
a + b
end
def add(a, b)
a + b
end
Benchmark.ips do |_|
_.report('contract') { add_c(3, 4) }
_.report('no contract') { add(3, 4) }
_.compare!
end
# pavel@dt3:~/prizeo/contract|⇒ bundle exec ruby add.rb
# Calculating -------------------------------------
# contract 22.437k i/100ms
# no contract 116.177k i/100ms
# -------------------------------------------------
# contract 282.640k (± 4.5%) i/s - 1.414M
# no contract 6.450M (± 8.6%) i/s - 32.065M
#
# Comparison:
# no contract: 6450153.1 i/s
# contract: 282640.5 i/s - 22.82x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment