Created
June 1, 2010 10:25
-
-
Save jodosha/420787 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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