Skip to content

Instantly share code, notes, and snippets.

@ukd1
Last active December 15, 2015 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ukd1/5268131 to your computer and use it in GitHub Desktop.
Save ukd1/5268131 to your computer and use it in GitHub Desktop.
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