Skip to content

Instantly share code, notes, and snippets.

@dwbutler
Last active August 29, 2015 14:01
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 dwbutler/51657198746c93103e08 to your computer and use it in GitHub Desktop.
Save dwbutler/51657198746c93103e08 to your computer and use it in GitHub Desktop.
Parsing ISO8601 Time format with Ruby
require 'time'
require 'benchmark'
time = Time.now.iso8601(3)
# => "2014-05-26T23:26:08.628-07:00"
Benchmark.bm do |x|
x.report('parse') { 100_000.times { Time.parse(time).gmtime } }
x.report('iso8601') { 100_000.times { Time.iso8601(time).gmtime } }
end
# MRI 2.1.2
# user system total real
# parse 2.890000 0.050000 2.940000 ( 2.938882)
# iso8601 1.130000 0.010000 1.140000 ( 1.144048)
# JRuby 1.7.12 (Stablized after a few runs)
# user system total real
# parse 4.810000 0.010000 4.820000 ( 4.701000)
# iso8601 0.750000 0.000000 0.750000 ( 0.715000)
require 'benchmark'
require 'logstash/event'
time = Time.now.iso8601(3)
# => "2014-05-26T23:35:53.961-07:00"
Benchmark.bm do |x|
x.report('joda') { 100_000.times { LogStash::Time.parse_iso8601(time) } }
end
# JRuby 1.7.12 (Stabilized after a few runs)
# user system total real
# joda 0.230000 0.000000 0.230000 ( 0.222000)
@colinsurprenant
Copy link

with JRuby use Benchmark.bmbm so it actually runs the benchmark twice, first as a"rehearsal" then the actual test so that the JVM gets warmed.

@dwbutler
Copy link
Author

Oh interesting. I didn't know about that, thanks!

The results presented here are actually the "stablized" value after running the benchmarks a few times. I'll remember to use Benchmark.bmbm in the future to automate this!

I'll check this now and see if I get significantly different results.

@dwbutler
Copy link
Author

I tried out Benchmark.bmbm and got similar results. I looked at this gist more closely and noticed that I did already put a comment in there: # JRuby 1.7.12 (Stabilized after a few runs)

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