Skip to content

Instantly share code, notes, and snippets.

@bogdan
Created December 25, 2011 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bogdan/1519380 to your computer and use it in GitHub Desktop.
Save bogdan/1519380 to your computer and use it in GitHub Desktop.
require "benchmark"
load 'old_callbacks.rb'
load '../lib/active_support/callbacks.rb'
GC.disable
module Definition
def self.included(base)
base.class_eval do
define_callbacks :save
10.times do
set_callback :save, :before, :before_save1
set_callback :save, :after, :after_save1
set_callback :save, :around, :around_save1
end
def before_save1;nil; end
def after_save1; nil; end
def around_save1; nil; end
def save
run_callbacks :save do
true
end
end
end
end
end
class OldClass
include ActiveSupport::OldCallbacks
end
class NewClass
include ActiveSupport::Callbacks
end
amount = 100
Benchmark.bmbm do |x|
x.report "New set_callback" do
amount.times do
Class.new do
include ActiveSupport::Callbacks
include Definition
end
end
end
x.report "Old set_callback" do
amount.times do
Class.new do
include ActiveSupport::OldCallbacks
include Definition
end
end
end
end
puts "*" * 120
class OldClass
include ActiveSupport::OldCallbacks
include Definition
end
old = OldClass.new
class NewClass
include ActiveSupport::OldCallbacks
include Definition
end
new = NewClass.new
amount = 10000
Benchmark.bmbm do |x|
x.report "New run_callbacks" do
amount.times do
new.save
end
end
x.report "Old run_callbacks" do
amount.times do
old.save
end
end
end
GC.enable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment