Last active
December 15, 2015 13:29
-
-
Save ukd1/5268131 to your computer and use it in GitHub Desktop.
This file contains 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
class GetEnv | |
def self.method_missing meth, key | |
v = ENV[key].to_i | |
return v if v.to_s == ENV[key] | |
v = ENV[key].to_f | |
return v if v.to_s == ENV[key] | |
ENV[key] | |
end | |
end | |
# | |
# Benchmarks | |
# | |
# Assumes the following environment variables exist; just run: | |
# export FLOAT=0.5 INT=2000 STRING=Testing | |
require 'benchmark' | |
include Benchmark | |
n = 500_000 | |
Benchmark.benchmark(CAPTION, 7, FORMAT, ">total:", ">avg:") do |x| | |
x.report('Native string') { for i in 1..n; ENV['STRING']; end } | |
x.report('GetEnv string') { for i in 1..n; GetEnv['STRING']; end } | |
x.report('Native integer') { for i in 1..n; ENV['INT'].to_i; end } | |
x.report('GetEnv integer') { for i in 1..n; GetEnv['INT']; end } | |
x.report('Native float') { for i in 1..n; ENV['FLOAT'].to_f; end } | |
x.report('GetEnv float') { for i in 1..n; GetEnv['FLOAT']; end } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment