Skip to content

Instantly share code, notes, and snippets.

@rafmagana
Created January 17, 2011 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rafmagana/783558 to your computer and use it in GitHub Desktop.
Save rafmagana/783558 to your computer and use it in GitHub Desktop.
Benchmark to see which is faster, either when *array or array.include?
require 'benchmark'
class Dummy
@VALID_OPTIONS = :first, :last, :all
class << self
def when_find(*args)
case args.first
when *@VALID_OPTIONS
send(args.first)
else
find_with_ids(*args)
end
end
def include_find(*args)
if @VALID_OPTIONS.include? args.first
send(args.first)
else
find_with_ids(*args)
end
end
def first;end
def last;end
def all;end
def find_with_ids(*args);end
end
end
Benchmark.bmbm do |bm|
bm.report("first with when:") { Dummy.when_find :first }
bm.report("first with include?:") { Dummy.include_find :first }
bm.report("last with when:") { Dummy.when_find :last }
bm.report("last with include?:") { Dummy.include_find :last }
bm.report("all with when:") { Dummy.when_find :all }
bm.report("all with include?:") { Dummy.include_find :all }
bm.report("ids with when:") { Dummy.when_find [1,2,3] }
bm.report("ids with include?:") { Dummy.include_find [1,2,3] }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment