I am writing code using R reference classes . However, as I progressed, the program became unbearably slow. To demonstrate the problem, do 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 is required:
- 4s for method calls
- 7s for calls to generate a random number
- 19s to increase the value of the field
This is the last result that causes me problems. I obviously do not expect it to take twice as much to increase a number than to generate a random number. My code includes a lot of access and changing the values โโof the fields in the reference class, and this performance problem made the program accessible to everyone.
: -, , / R? -, -?