Skip to content

Instantly share code, notes, and snippets.

@sambostock
Created February 15, 2019 04:14
Show Gist options
  • Save sambostock/72fe626c6e64ef2b2d38452ae857a783 to your computer and use it in GitHub Desktop.
Save sambostock/72fe626c6e64ef2b2d38452ae857a783 to your computer and use it in GitHub Desktop.
Benchmarking Timecop vs ActiveSupport::Testing::TimeHelpers
# frozen_string_literal: true
ruby '2.5.3'
source "https://rubygems.org"
gem 'timecop'
gem 'activesupport'
GEM
remote: https://rubygems.org/
specs:
activesupport (5.2.2)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
concurrent-ruby (1.1.4)
i18n (1.5.3)
concurrent-ruby (~> 1.0)
minitest (5.11.3)
thread_safe (0.3.6)
timecop (0.9.1)
tzinfo (1.2.5)
thread_safe (~> 0.1)
PLATFORMS
ruby
DEPENDENCIES
activesupport
timecop
RUBY VERSION
ruby 2.5.3p105
BUNDLED WITH
1.17.3
Timecop vs ActiveSupport::Testing::TimeHelpers
100000 trials each
user system total real
Timecop.freeze { } 0.763503 0.002264 0.765767 ( 0.767017)
freeze_time { } 3.617230 0.031886 3.649116 ( 3.706262)
Timecop.freeze and Timecop.return 0.939708 0.005576 0.945284 ( 0.958756)
freeze_time and travel_back 2.707030 0.018956 2.725986 ( 2.741195)
# frozen_string_literal: true
require 'timecop'
require 'active_support/testing/time_helpers'
require 'benchmark'
extend ActiveSupport::Testing::TimeHelpers
trials = 100_000
label_width = 32
puts 'Timecop vs ActiveSupport::Testing::TimeHelpers'
puts "#{trials} trials each"
Benchmark.bm(label_width) do |x|
x.report('Timecop.freeze { }') do
trials.times do
Timecop.freeze { }
end
end
x.report('freeze_time { }') do
trials.times do
freeze_time { }
end
end
x.report('Timecop.freeze and Timecop.return') do
trials.times do
Timecop.freeze and Timecop.return
end
end
x.report('freeze_time and travel_back') do
trials.times do
freeze_time and travel_back
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment