Last active
August 11, 2016 17:58
-
-
Save johanlunds/af6c0b5d179a343464cad6dcd585e76a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# gem install benchmark-ips | |
require 'benchmark/ips' | |
require 'ruby-measurement' | |
require 'ruby-units' | |
Measurement.define(:tempC) do |unit| | |
unit.convert_to(:tempK) { |value| value + 273.15 } | |
end | |
Measurement.define(:tempK) do |unit| | |
end | |
Measurement.define(:millibar) do |unit| | |
unit.convert_to(:Pa) { |value| value * 100 } | |
end | |
Measurement.define(:Pa) do |unit| | |
end | |
kmh_to_ms_factor = Rational(1000, 3600) | |
Measurement.define('km/h') do |unit| | |
unit.convert_to('m/s') { |value| value * kmh_to_ms_factor } | |
end | |
Measurement.define('m/s') do |unit| | |
end | |
Benchmark.ips do |x| | |
x.report("ruby-measurement") do | |
Measurement.new(323.5, 'tempC').convert_to('tempK').quantity | |
Measurement.new(323.5, 'millibar').convert_to('Pa').quantity | |
Measurement.new(323.5, 'km/h').convert_to('m/s').quantity | |
Measurement.new(323.5, 'tempC').convert_to('tempK') | |
Measurement.new(323.5, 'km/h').convert_to('m/s') | |
end | |
x.report("ruby-units") do | |
Unit.new(323.5, 'tempC').convert_to('tempK').scalar | |
Unit.new(323.5, 'millibar').convert_to('Pa').scalar | |
Unit.new(323.5, 'km/h').convert_to('m/s').scalar | |
Unit.new(323.5, 'tempC').convert_to('tempK') | |
Unit.new(323.5, 'km/h').convert_to('m/s') | |
end | |
x.compare! | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ruby bm.rb | |
Warming up -------------------------------------- | |
ruby-measurement 5.713k i/100ms | |
ruby-units 6.000 i/100ms | |
Calculating ------------------------------------- | |
ruby-measurement 63.247k (± 2.4%) i/s - 319.928k in 5.061386s | |
ruby-units 64.435 (± 3.1%) i/s - 324.000 in 5.031803s | |
Comparison: | |
ruby-measurement: 63247.3 i/s | |
ruby-units: 64.4 i/s - 981.57x slower | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment