Skip to content

Instantly share code, notes, and snippets.

@Erol
Last active August 29, 2015 14:12
Show Gist options
  • Save Erol/aa29dfec253cbdb4d481 to your computer and use it in GitHub Desktop.
Save Erol/aa29dfec253cbdb4d481 to your computer and use it in GitHub Desktop.
Kernel#load vs Kernel#eval
require 'benchmark'
Benchmark.bmbm do |bm|
bm.report '1000 x Kernel#load wrap=false' do
1000.times do
load 'script.rb', false
end
end
bm.report '1000 x Kernel#load wrap=true' do
1000.times do
load 'script.rb', true
end
end
bm.report '1000 x Kernel#eval' do
1000.times do
eval File.read 'script.rb'
end
end
end
Rehearsal -----------------------------------------------------------------
1000 x Kernel#load wrap=false 0.660000 0.060000 0.720000 ( 0.720393)
1000 x Kernel#load wrap=true 0.780000 0.060000 0.840000 ( 0.848723)
1000 x Kernel#eval 0.600000 0.020000 0.620000 ( 0.628405)
-------------------------------------------------------- total: 2.180000sec
user system total real
1000 x Kernel#load wrap=false 0.640000 0.060000 0.700000 ( 0.693713)
1000 x Kernel#load wrap=true 0.800000 0.060000 0.860000 ( 0.861577)
1000 x Kernel#eval 0.580000 0.020000 0.600000 ( 0.603365)
ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-darwin14.0]
require 'set'
module FizzBuzz
def fizz?
self % 3 == 0
end
def buzz?
self % 5 == 0
end
end
class Fixnum
include FizzBuzz
end
fizz = Set.new
buzz = Set.new
1000.times do |i|
fizz.add i if i.fizz?
buzz.add i if i.buzz?
end
fizzbuzz = fizz & buzz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment