Skip to content

Instantly share code, notes, and snippets.

@trans
Created March 4, 2011 15:53
Show Gist options
  • Save trans/854852 to your computer and use it in GitHub Desktop.
Save trans/854852 to your computer and use it in GitHub Desktop.
Benchmark reading with and without ||= assignment.
# Run 1000000 Times:
#
# ruby 1.8.7 (2010-01-10 patchlevel 249) [x86_64-linux]
#
# Simple Read : 0.217292 seconds
# Or Equals : 0.31042 seconds
#
# ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-linux]
#
# Simple Read : 0.113411725 seconds
# Or Equals : 0.174376353 seconds
n = 1000000
puts "#{n} Times"
t = Time.now
@b = :b
n.times do
@b
end
puts "Simple Read : #{Time.now - t} seconds"
t = Time.now
n.times do
@a ||= :a
end
puts " Or Equals : #{Time.now - t} seconds"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment