Skip to content

Instantly share code, notes, and snippets.

@joel
Created June 14, 2012 06:59
Show Gist options
  • Save joel/2928524 to your computer and use it in GitHub Desktop.
Save joel/2928524 to your computer and use it in GitHub Desktop.
Time comparison ActiveSupport vs Gem Twitter Static Time Constants w Ruby Benchmark
require 'rubygems'
# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('Gemfile', __FILE__)
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
require 'rails/all'
require 'time_constants'
N = 1000000
to = Time.local(2008, 12, 27, 14, 30, 59)
from = Time.local(2008, 12, 29, 14, 30, 59)
# from.day - to.day => 2
# know number of hours since to now
Benchmark.bmbm do |x|
x.report('with active support') { N.times {
age = from.day - Time.now.day
case
when age < 7
'green' # Normally
when age > 15
'red' # Critical
else
'orange' # On transition
end
} }
x.report('with constants w .to_i') { N.times {
age = from.to_i - Time.now.to_i
case
when age < T_7_DAYS
'green' # Normally
when age > T_15_DAYS
'red' # Critical
else
'orange' # On transition
end
} }
x.report('with constants') { N.times {
age = from - Time.now
case
when age < T_7_DAYS
'green' # Normally
when age > T_15_DAYS
'red' # Critical
else
'orange' # On transition
end
} }
end
# user system total real
# with active support 5.710000 0.030000 5.740000 ( 6.170684)
# with constants w .to_i 1.870000 0.020000 1.890000 ( 1.975625)
# with constants 3.990000 0.020000 4.010000 ( 4.410851)
@mcansky
Copy link

mcansky commented Jun 15, 2012

ohohoho impressive love this please add to wiki

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment