相关疑难解决方法(0)

Ruby attr_accessor与getter/setter基准测试:为什么访问器更快?

我刚刚对等效的getter/setter方法测试了attr_accessor:

class A
  # we define two R/W attributes with accessors
  attr_accessor :acc, :bcc

  # we define two attributes with getter/setter-functions
  def dirA=(d); @dirA=d; end
  def dirA; @dirA; end
  def dirB=(d); @dirB=d; end
  def dirB; @dirB; end
end

varA   = A.new
startT = 0
dirT   = 0
accT   = 0

# now we do 100 times the same benchmarking
# where we do the same assignment operation
# 50000 times
100.times do
  startT = Time.now.to_f
  50000.times do |i|
    varA.dirA = …
Run Code Online (Sandbox Code Playgroud)

ruby benchmarking attr-accessor getter-setter

10
推荐指数
1
解决办法
1686
查看次数

标签 统计

attr-accessor ×1

benchmarking ×1

getter-setter ×1

ruby ×1