Last active
December 20, 2015 11:49
-
-
Save jhoblitt/6126221 to your computer and use it in GitHub Desktop.
Facter::Util::Resolution#which memoization benchmark
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
#!/usr/bin/env ruby | |
require 'facter/util/resolution' | |
require 'benchmark' | |
@foo = {} | |
def wrap_which(bin) | |
if @foo.has_key?(bin) | |
return @foo[bin] | |
else | |
bob = Facter::Util::Resolution.which(bin) | |
@foo[bin] = bob | |
return bob | |
end | |
end | |
@bar = {} | |
def wrap_which_sym(bin) | |
sym = bin.to_sym | |
if @bar.has_key?(sym) | |
return @bar[sym] | |
else | |
bob = Facter::Util::Resolution.which(bin) | |
@bar[sym] = bob | |
return bob | |
end | |
end | |
n = 10_000 | |
Benchmark.bm do |b| | |
b.report { | |
for i in 1 .. n | |
Facter::Util::Resolution.which('ls') | |
end | |
} | |
b.report { | |
for i in 1 .. n | |
wrap_which('ls') | |
end | |
} | |
b.report { | |
for i in 1 .. n | |
wrap_which_sym('ls') | |
end | |
} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment