Created
November 19, 2010 23:05
-
-
Save cmer/707372 to your computer and use it in GitHub Desktop.
YAML, ZAML, Marshal & JSON quick'n'dirty benchmark.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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