Last active
August 29, 2015 14:00
-
-
Save FaviusTy/41ddf2bdf17f2caaa649 to your computer and use it in GitHub Desktop.
json,oj,msgpackの比較ベンチマーク
This file contains 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
# 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'] |
messagepack速い。pstore噛ましてた昔の自分、超愚か。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
計測結果