Skip to content

Instantly share code, notes, and snippets.

@bogdan
Created May 16, 2012 13:51
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/2710476 to your computer and use it in GitHub Desktop.
Save bogdan/2710476 to your computer and use it in GitHub Desktop.
$:.unshift("./lib")
require "active_support/callbacks"
require "diffbench"
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(key = nil)
run_callbacks :save do
true
end
end
end
end
end
class NewClass
include ActiveSupport::Callbacks
include Definition
end
new = NewClass.new
amount = 10
DiffBench.bm do |x|
x.report "set_callback" do
amount.times do
Class.new do
include ActiveSupport::Callbacks
include Definition
end
end
end
x.report "define_callbacks" do
new_klass = Class.new do
include ActiveSupport::Callbacks
end
amount = 100
amount.times do |i|
new_klass.send(:define_callbacks, :"save#{i}")
end
end
x.report "run_callbacks" do
amount = 1000
amount.times do
new.save
end
end
x.report "skip_callback" do
amount = 100
klass = Class.new do
include ActiveSupport::Callbacks
include Definition
end
amount.times do |index|
klass.skip_callback :save, :before, :before_save1, :if => "false"
end
end
end
GC.enable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment