Skip to content

Instantly share code, notes, and snippets.

@Asher-
Forked from deepak/benchmark_env.rb
Created November 22, 2010 13:35
Show Gist options
  • Save Asher-/709980 to your computer and use it in GitHub Desktop.
Save Asher-/709980 to your computer and use it in GitHub Desktop.
#https://gist.github.com/709860
require 'benchmark'
# does Benchmark::bmbm have environments/setup like Test::Unit
# want some variables to be set per benchmark case - now have to dup them myself
# is is needed as the testcases have destructive operations, otherwise the testcases will be dependent on each other
def benchmark_setup(obj=TOP_SELF)
code = <<-CODE
big = (1..95).to_a
sml = (75..110).to_a
CODE
# passing code in lambda does not modify but string does
# obj.instance_eval {
# big = (1..95).to_a
# sml = (75..110).to_a
# }
obj.instance_eval(code, __FILE__, __LINE__)
end
TOP_SELF = self
module Benchmark
alias_method :org_measure, :measure
def measure(label="", &blk)
benchmark_setup(TOP_SELF)
org_measure(label, &blk)
end
module_function :org_measure
end
Benchmark.bmbm { |x|
x.report("iterate_larger") {
#big = big_arr.dup
#sml = sml_arr.dup
big.each do |r|
sml.delete_if {|s| s == r }
end
}
}
Benchmark.bmbm { |x|
x.report("set_diff") {
#big = big_arr.dup
#sml = sml_arr.dup
sml - big
}
}
Benchmark.bmbm { |x|
x.report("iterate_smaller") {
#big = big_arr.dup
#sml = sml_arr.dup
sml.collect do |s|
s unless big.include?(x)
end.compact
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment