问题
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