开发者

Ruby Struct - Performance Overhead?

开发者 https://www.devze.com 2023-01-05 20:22 出处:网络
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 comme

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

精彩评论

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