Skip to content

Instantly share code, notes, and snippets.

@jhoblitt
Last active December 20, 2015 11:49
Show Gist options
  • Save jhoblitt/6126221 to your computer and use it in GitHub Desktop.
Save jhoblitt/6126221 to your computer and use it in GitHub Desktop.
Facter::Util::Resolution#which memoization benchmark
#!/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