Speeding up field access in R reference classes

时光总嘲笑我的痴心妄想 提交于 2019-12-23 12:08:04

问题


I have been writing code using R reference classes. However, as I have progressed, the program has become intolerably slow. To demonstrate the problem, take the following example:

myClass <- setRefClass(
  "Class"="myClass",
  fields=c(
    counter="numeric"
  ),
  methods=list(
    initialize=function () {
      counter <<- 0
    },
    donothing=function () {

    },
    rand=function () {
      return(runif(1))
    },
    increment=function () {
      counter <<- counter + 1
    }
  )
)

mc <- myClass()
system.time(for (it in 1:500000) {
  mc$donothing()
})
system.time(for (it in 1:500000) {
  mc$rand()
})
system.time(for (it in 1:500000) {
  mc$increment()
})

It takes:

  • 4s for calls to a method
  • 7s for calls to generate a random number
  • 19s to increment a field value

It's the last result that's causing me problems. I obviously don't expect it to take twice as long to increment a number than to generate a random number. My code involves a lot of accessing and changing of field values in a reference class, and this performance issue has made the program all but usable.

My question: is there anything I can do to improve the performance of field lookup/access in R reference classes? Is there anything I should be doing differently?


回答1:


It seems a major performance issue was due to providing class names in the fields argument. If I replace

fields=c(
    counter="numeric"
),

with

fields=c("counter")

the calculation completes in 5s, compared to 19s. It is difficult to determine from the documentation why the performance penalty is so great -- perhaps it is due to checking of classes during assignment. The documentation mentions the following:

In particular, fields with a specified class are implemented as a special form of active binding to enforce valid assignment to the field

I'm not too sure what 'active binding' is, but I assume it introduces some pre-assignment logic.



来源:https://stackoverflow.com/questions/21665838/speeding-up-field-access-in-r-reference-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!