Skip to content

Instantly share code, notes, and snippets.

@bradland
Last active Aug 29, 2015
Embed
What would you like to do?
#!/usr/bin/env ruby
require 'benchmark/ips'
require 'time'
if defined?(JRUBY_VERSION)
JODA_FMT = org.joda.time.format::DateTimeFormat.for_pattern("Y-M-d H:m:s Z")
end
def joda_time(time)
d = JODA_FMT.parseLocalTime(time).to_date_time_today
DateTime.new!(d)
end
Benchmark.ips do |x|
# Config
x.time = 20
x.warmup = 10
# Inputs
TIME = "2011-05-25 00:00:03 -0400".freeze
FMT = "%Y-%m-%d %H:%M:%S %z".freeze
# Benchmark
x.report('joda:') { joda_time(TIME) } if defined?(JRUBY_VERSION)
x.report('strptime:') { Time.strptime(TIME, FMT) }
x.report('parse:') { Time.parse(TIME) }
# Report
x.compare!
end
@bradland

This comment has been minimized.

Copy link
Owner Author

@bradland bradland commented Jan 2, 2015

JRuby 1.7.17:

$ ruby datebench-ips.rb 
Calculating -------------------------------------
               joda:    20.651k i/100ms
           strptime:     1.685k i/100ms
              parse:     2.289k i/100ms
-------------------------------------------------
               joda:    250.435k (± 9.5%) i/s -      4.936M
           strptime:     19.337k (± 2.7%) i/s -    387.550k
              parse:     24.881k (± 1.7%) i/s -    499.002k

Comparison:
               joda::   250434.7 i/s
              parse::    24880.6 i/s - 10.07x slower
           strptime::    19336.8 i/s - 12.95x slower

MRI 2.1.3:

$ ruby datebench-ips.rb 
Calculating -------------------------------------
           strptime:     8.054k i/100ms
              parse:     3.984k i/100ms
-------------------------------------------------
           strptime:     84.628k (± 2.6%) i/s -      1.699M
              parse:     40.625k (± 5.0%) i/s -    812.736k

Comparison:
           strptime::    84628.5 i/s
              parse::    40624.7 i/s - 2.08x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment