Skip to content

Instantly share code, notes, and snippets.

@cmer
Created November 19, 2010 23:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cmer/707372 to your computer and use it in GitHub Desktop.
Save cmer/707372 to your computer and use it in GitHub Desktop.
YAML, ZAML, Marshal & JSON quick'n'dirty benchmark.rb
require 'rubygems'
require 'benchmark'
require 'yaml'
require 'zaml'
require 'json'
yaml_str = <<-eos
yaml goes here
eos
h = nil
GC.start
yaml_load_time = Benchmark::realtime {
1000.times { h = YAML.load(yaml_str) }
}
GC.start
to_yaml_time = Benchmark::realtime {
1000.times { h.to_yaml }
}
begin
YAML::ENGINE.yamler = 'psych' if RUBY_VERSION == "1.9.2"
rescue
puts "Cannot load Psych. Is libyaml installed? Install it and recompile Ruby."
puts "On OS X with RVM, install it with MacPorts and run:"
puts "rvm install ruby-1.9.2 -C --with-libyaml-dir=/opt/local"
exit 1
end
GC.start
psych_load_time = Benchmark::realtime {
1000.times { YAML.load(yaml_str) }
}
GC.start
to_yaml_psych_time = Benchmark::realtime {
1000.times { h.to_yaml }
}
GC.start
to_json_time = Benchmark::realtime {
1000.times { h.to_json }
}
GC.start
zaml_dump_time = Benchmark::realtime {
1000.times { ZAML.dump(h) }
}
marshaled = nil
GC.start
marshal_dump_time = Benchmark::realtime {
1000.times { marshaled = Marshal.dump(h) }
}
GC.start
marshall_load_time = Benchmark::realtime {
1000.times { h = Marshal.load(marshaled) }
}
puts `ruby -v`
puts "--------------------------------------------------------------------"
puts "YAML.load:\t\t#{'%.4f' % yaml_load_time}"
puts "YAML.load (Psych):\t#{'%.4f' % psych_load_time}" if RUBY_VERSION == "1.9.2"
puts "Marshal.load:\t\t#{'%.4f' % marshall_load_time}"
puts "to_yaml:\t\t#{'%.4f' % to_yaml_time}"
puts "to_load (Psych):\t#{'%.4f' % to_yaml_psych_time}" if RUBY_VERSION == "1.9.2"
puts "ZAML.dump:\t\t#{'%.4f' % zaml_dump_time}"
puts "to_json:\t\t#{'%.4f' % to_json_time}"
puts "Marshal.dump:\t\t#{'%.4f' % marshal_dump_time}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment