Created
January 17, 2013 15:18
-
-
Save FaviusTy/4556658 to your computer and use it in GitHub Desktop.
ruby 1.9 環境でPStoreとJsonファイルのI/O処理時間を比較してみた
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 "pstore" | |
require "json" | |
require "benchmark" | |
_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]}, | |
bool: true | |
} | |
data = {} | |
read_obj = nil | |
(1..30000).each do |cycle| | |
data["#{cycle}"] = _data | |
end | |
Benchmark.bm(13) do |x| | |
x.report("write: "){ | |
db = PStore.new("./backup") | |
db.transaction do | |
db["root"] = data | |
end | |
} | |
x.report("read: "){ | |
db = PStore.new("./backup") | |
db.transaction(true) do | |
read_obj = db["root"] | |
end | |
} | |
end | |
p read_obj["1"][:test] | |
p read_obj["#{read_obj.size}"][:arr] | |
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 read_obj["#{read_obj.size}"]["arr"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
計測結果の一例
PStore:
user system total real
write: 0.032000 0.000000 0.032000 ( 0.061003)
read: 0.046000 0.000000 0.046000 ( 0.054003)
JSONFile:
user system total real
write: 0.546000 0.031000 0.577000 ( 0.607035)
read: 0.812000 0.047000 0.859000 ( 0.849049)