Skip to content

Instantly share code, notes, and snippets.

@Asher-
Forked from deepak/benchmark_env.rb
Created November 22, 2010 13:49
Show Gist options
  • Save Asher-/709998 to your computer and use it in GitHub Desktop.
Save Asher-/709998 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
larger_benchmark = Proc.new do |benchmark|
big = (1..95).to_a
sml = (75..110).to_a
benchmark.report("iterate_larger") {
big.each do |big_member|
sml.delete_if {|sml_member| sml_member == big_member }
end
}
end
diff_benchmark = Proc.new do |benchmark|
big = (1..95).to_a
sml = (75..110).to_a
benchmark.report("set_diff") { sml - big }
end
smaller_benchmark = Proc.new do |benchmark|
big = (1..95).to_a
sml = (75..110).to_a
benchmark.report("iterate_smaller") { sml.collect { |sml_member| sml_member unless big.include?( sml_member ) }.compact }
end
benchmark_procs = [ larger_benchmark, diff_benchmark, smaller_benchmark ]
benchmark_procs.each do |proc|
Benchmark.bmbm( & proc )
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment