Skip to content

Instantly share code, notes, and snippets.

@bastjan
Forked from pauldix/gist:7549
Last active August 29, 2015 14:11
Show Gist options
  • Save bastjan/e69a103480da720cd3d3 to your computer and use it in GitHub Desktop.
Save bastjan/e69a103480da720cd3d3 to your computer and use it in GitHub Desktop.
Benchmark different ruby serializing options
require 'benchmark'
require 'rubygems'
require 'json'
require 'yaml'
include Benchmark
benchmark_iterations = 1
large_single_dimension_array = [42, 123.123] * 5000
large_single_dimension_hash = {}
10000.times do |i|
large_single_dimension_hash["key#{i}".intern] = i
end
benchmark do |t|
t.report("array marshal") do
benchmark_iterations.times {
Marshal.load(Marshal.dump(large_single_dimension_array))
}
end
t.report("array json ") do
benchmark_iterations.times {
JSON.load(JSON.dump(large_single_dimension_array))
}
end
t.report("array yaml ") do
benchmark_iterations.times {
YAML.load(YAML.dump(large_single_dimension_array))
}
end
t.report("hash marshal ") do
benchmark_iterations.times {
Marshal.load(Marshal.dump(large_single_dimension_hash))
}
end
t.report("hash json ") do
benchmark_iterations.times {
JSON.load(JSON.dump(large_single_dimension_hash))
}
end
t.report("hash yaml ") do
benchmark_iterations.times {
YAML.load(YAML.dump(large_single_dimension_hash))
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment