Created
November 3, 2013 03:43
-
-
Save litch/7286423 to your computer and use it in GitHub Desktop.
Slightly surprisingly small amount of performance difference for these three methods of getting things done...
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
require 'redis' | |
require 'benchmark' | |
module ModuleMethods | |
def self.record_metric(name) | |
$REDIS.lpush(translate_name(name), Time.now.to_f) | |
end | |
def self.translate_name(queue) | |
queue.to_s.start_with?('adsentry:') ? queue : "adsentry:#{queue}" | |
end | |
end | |
class ClassMethods | |
def self.record_metric(name) | |
$REDIS.lpush(translate_name(name), Time.now.to_f) | |
end | |
def self.translate_name(queue) | |
queue.to_s.start_with?('adsentry:') ? queue : "adsentry:#{queue}" | |
end | |
end | |
class InstanceMethods | |
def initialize(name) | |
translate_name(name) | |
end | |
def record_metric | |
$REDIS.lpush(queue_name, Time.now.to_f) | |
end | |
private | |
attr_reader :queue_name | |
def translate_name(queue) | |
@queue_name = queue.to_s.start_with?('adsentry:') ? queue : "adsentry:#{queue}" | |
end | |
end | |
$REDIS = Redis.new | |
Benchmark.bmbm do |x| | |
x.report("module") { 100000.times { ModuleMethods.record_metric(:module) }} | |
x.report("class") { 100000.times { ClassMethods.record_metric(:class) }} | |
x.report("instance") { 100000.times { InstanceMethods.new(:instance).record_metric }} | |
end | |
# Rehearsal -------------------------------------------- | |
# module 5.440000 2.340000 7.780000 ( 9.761189) | |
# class 5.490000 2.340000 7.830000 ( 9.808614) | |
# instance 5.550000 2.350000 7.900000 ( 9.888947) | |
# ---------------------------------- total: 23.510000sec | |
# user system total real | |
# module 5.470000 2.330000 7.800000 ( 9.771679) | |
# class 5.470000 2.340000 7.810000 ( 9.786765) | |
# instance 5.510000 2.340000 7.850000 ( 9.815554) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment