开发者

Ruby Struct - Performance Overhead?

Is there any performance overhead that you take on when using S开发者_如何学Gotructs (as compared to Arrays, Hashes, etc.) in Ruby?


I tried running the following several times (following Jörg's comment this has been updated to use a fixed variable for the value instead of creating lots of strings):

require 'benchmark'

Example = Struct.new("Example", :value)

struct = Example.new
hash = {}
value = "The value"

n = 5000000
Benchmark.bm do |m|
  # test assignment and access for Hash and Struct
  m.report { n.times do; hash[:value] = value; end }
  m.report { n.times do; struct.value = value; end }
end

Update

It seems with a large enough value of n the Struct is slightly slower but I can't imagine this would be noticeable or an issue in practice.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜