Skip to content

Instantly share code, notes, and snippets.

@jodosha
Created June 1, 2010 10:25
Show Gist options
  • Save jodosha/420787 to your computer and use it in GitHub Desktop.
Save jodosha/420787 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby -w
require "rubygems"
require "benchmark"
require "active_support"
TIMES = 10_000_000
class Project
attr_reader :ivar
def initialize
@ivar = nil
end
def check_with_cast
ivar ? true : false
end
def check_with_present
ivar.present?
end
end
project = Project.new
Benchmark.bm(30) do |b|
b.report "check_with_cast" do
TIMES.times do |i|
project.check_with_cast
end
end
b.report "check_with_present" do
TIMES.times do |i|
project.check_with_present
end
end
end
__END__
user system total real
check_with_cast 3.520000 0.000000 3.520000 ( 3.548733)
check_with_present 6.640000 0.000000 6.640000 ( 6.669177)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment