Skip to content

Instantly share code, notes, and snippets.

@FaviusTy
Last active August 29, 2015 14:00
Show Gist options
  • Save FaviusTy/41ddf2bdf17f2caaa649 to your computer and use it in GitHub Desktop.
Save FaviusTy/41ddf2bdf17f2caaa649 to your computer and use it in GitHub Desktop.
json,oj,msgpackの比較ベンチマーク
# encoding: utf-8
require 'json'
require 'oj'
require 'benchmark'
require 'msgpack'
_data = {test: 'testdatatestdatatestdata',
data: 18358374659302536,
_float: 0.1332542572647585,
arr: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,],
nested: {key: "key", arr: [1, 1, 1, 000, 32435, true, false], 'text' => 'test-test-test-test-data'},
bool: true
}
data = {}
read_obj = nil
(1..200000).each do |cycle|
data["#{cycle}"] = _data
end
p 'json'
Benchmark.bm(13) do |x|
x.report('write: ') {
File.open('json', 'w') do |f|
f.flock(File::LOCK_EX)
f.write JSON.generate(data)
f.flock(File::LOCK_UN)
end
}
x.report('read: ') {
File.open('json', 'r') do |f|
f.flock(File::LOCK_EX)
read_obj = JSON.load(f)
f.flock(File::LOCK_UN)
end
}
end
p read_obj['1']['test']
p 'oj'
Benchmark.bm(13) do |x|
x.report('write: ') {
File.open('oj', 'w') do |f|
f.flock(File::LOCK_EX)
f.write Oj.dump data, mode: :compat
f.flock(File::LOCK_UN)
end
}
x.report('read: ') {
File.open('oj', 'r') do |f|
f.flock(File::LOCK_EX)
read_obj = Oj.load f
f.flock(File::LOCK_UN)
end
}
end
p read_obj['1']['test']
p 'msgpack'
Benchmark.bm(13) do |x|
x.report('write: ') {
File.open('msgpack', 'w') do |f|
f.flock(File::LOCK_EX)
f.write MessagePack.pack data
f.flock(File::LOCK_UN)
end
}
x.report('read: ') {
File.open('msgpack', 'r') do |f|
f.flock(File::LOCK_EX)
read_obj = MessagePack.unpack f
f.flock(File::LOCK_UN)
end
}
end
p read_obj['1']['test']
@FaviusTy
Copy link
Author

FaviusTy commented May 5, 2014

計測結果

"json"
                    user     system      total        real
write:          2.510000   0.100000   2.610000 (  2.687265)
read:           3.920000   0.420000   4.340000 (  4.388143)
"testdatatestdatatestdata"
"oj"
                    user     system      total        real
write:          1.130000   0.090000   1.220000 (  1.294805)
read:           3.030000   0.310000   3.340000 (  3.568096)
"testdatatestdatatestdata"
"msgpack"
                    user     system      total        real
write:          0.510000   0.060000   0.570000 (  0.608993)
read:           1.300000   0.200000   1.500000 (  1.619535)
"testdatatestdatatestdata"

@FaviusTy
Copy link
Author

FaviusTy commented May 5, 2014

messagepack速い。pstore噛ましてた昔の自分、超愚か。

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