Ruby PStore vs封送处理性能
我正在尝试决定是否在代码中实现Ruby PStore vs封送处理性能,ruby,marshalling,Ruby,Marshalling,我正在尝试决定是否在代码中实现PStore,而不是将各种对象保存为marshall。 以下代码试图比较这两种方法的性能: require 'pstore' def m_dump(filename, object) File.open(filename,"wb") do |f| Marshal.dump(object, f) end end def m_load(filename) object = nil File.open(filename,
PStore
,而不是将各种对象保存为marshall
。
以下代码试图比较这两种方法的性能:
require 'pstore'
def m_dump(filename, object)
File.open(filename,"wb") do |f|
Marshal.dump(object, f)
end
end
def m_load(filename)
object = nil
File.open(filename,'rb') do |f|
object = Marshal.load(f)
end
return object
end
test = {}
test[:pi] = [Math::PI]*100000000
start = Time.now
m_dump("test.marshal", test);nil
puts "Marshal File written in #{(Time.now - start).to_s}"
start = Time.now
results = PStore.new("test.pstore")
results.transaction{results[:test] = test};nil
puts "Pstore File written in #{(Time.now - start).to_s}"
start = Time.now
test2 = m_load("test.marshal");nil
puts "Marshal File read in #{(Time.now - start).to_s}"
start = Time.now
results2 = PStore.new("test.pstore")
test3 = results2.transaction{results2[:test]};nil
puts "Pstore read in #{(Time.now - start).to_s}"
puts "Marshal check #{test2 == test}"
puts "Pstore check #{test3 == test}"
运行代码会产生以下结果:
Marshal File written in 7.936485
Pstore File written in 5.526494
Marshal File read in 5.890848
Pstore read in 11.135965
创建新存档时,PStore
似乎稍微快一点,但在必须读取数据时则慢得多
考虑到PStore
是基于Marshal
的,我希望有类似的性能。这种行为是意料之中的吗 事务=>事务(真)
其中“true”表示只读。那么,表演将与马歇尔相似
start = Time.now
results2 = PStore.new("test.pstore")
test3 = results2.transaction(true) {results2[:test]}; nil
puts "Pstore read in #{(Time.now - start).to_s}"
事务=>事务(true)
其中“true”表示只读。那么,表演将与马歇尔相似
start = Time.now
results2 = PStore.new("test.pstore")
test3 = results2.transaction(true) {results2[:test]}; nil
puts "Pstore read in #{(Time.now - start).to_s}"
如果你关心速度,也可以看看公牛宝石。比较不同大小的文件的更新时间是很好的,因为散列通常是存储的。如果你担心速度的话,也可以看看OxGem。比较不同大小文件的更新时间是很好的,因为散列通常是存储的。