Skip to content

Instantly share code, notes, and snippets.

@filiptepper
Created March 24, 2017 20:49
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 filiptepper/cfacc53fc83a09a2cc2837279f508a56 to your computer and use it in GitHub Desktop.
Save filiptepper/cfacc53fc83a09a2cc2837279f508a56 to your computer and use it in GitHub Desktop.
Time benchmarks.rb
require "bundler/inline"
require "time"
require "date"
gemfile(true) do
source "https://rubygems.org/"
gem "benchmark-ips", require: "benchmark/ips"
end
time = "2017-02-28 14:07:15+0000"
Benchmark.ips do |x|
x.config warmup: 100 if RUBY_PLATFORM == "java"
x.report(%q{Time.strptime(time, "%Y-%m-%d %H:%M:%S%z").strftime("%s")}) do
Time.strptime(time, "%Y-%m-%d %H:%M:%S%z").strftime("%s")
end
x.report(%q{DateTime.strptime(time, "%Y-%m-%d %H:%M:%S%z").strftime("%s")}) do
DateTime.strptime(time, "%Y-%m-%d %H:%M:%S%z").strftime("%s")
end
x.report(%q{Time.strptime(time, "%Y-%m-%d %H:%M:%S%z").to_i}) do
Time.strptime(time, "%Y-%m-%d %H:%M:%S%z").to_i
end
x.report(%q{DateTime.strptime(time, "%Y-%m-%d %H:%M:%S%z").to_i}) do
DateTime.strptime(time, "%Y-%m-%d %H:%M:%S%z").to_time.to_i
end
x.report(%q{DateTime.parse(time).to_time.to_i}) do
DateTime.parse(time).to_time.to_i
end
x.report(%q{Time.parse(time).to_i}) do
Time.parse(time).to_i
end
x.compare!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment