Skip to content

Instantly share code, notes, and snippets.

Created December 14, 2012 11:49
Show Gist options
  • Save anonymous/4284879 to your computer and use it in GitHub Desktop.
Save anonymous/4284879 to your computer and use it in GitHub Desktop.
require 'benchmark'
require 'mysql2'
require 'yaml'
require 'json'
require 'msgpack'
client = Mysql2::Client.new(host: "localhost", username: "user", password: 'password', database: 'tests')
sql = 'SELECT * FROM table LIMIT 1000';
results = client.query(sql, as: :array)
$array = []
results.each do |row|
$array << row
end
def benchmark_serialize()
time = Benchmark.realtime do
puts "Time: #{bench[:time]} sec, Size: #{bench[:size]}"%
(1..1).each do |index|
@ser = yield($array)
end
end
{size: @ser.size, time: time}
end
puts "YAML:"
bench = benchmark_serialize() do |offers|
YAML::dump(offers)
end
puts "Time: #{bench[:time]} sec, Size: #{bench[:size]}"
puts "JSON:"
bench = benchmark_serialize() do |offers|
offers.to_json
end
puts "Time: #{bench[:time]} sec, Size: #{bench[:size]}"
puts "Marshal:"
bench = benchmark_serialize() do |offers|
Marshal::dump(offers)
end
puts "Time: #{bench[:time]} sec, Size: #{bench[:size]}"
puts "MsgPack:"
bench = benchmark_serialize() do |offers|
offers.to_msgpack
end
puts "Time: #{bench[:time]} sec, Size: #{bench[:size]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment