Converting package using S3 to S4 classes, is there going to be performance drop?

后端 未结 3 1000
执念已碎
执念已碎 2021-02-14 02:04

I have an R package which currently uses S3 class system, with two different classes and several methods for generic S3 functions like plot, logL

3条回答
  •  萌比男神i
    2021-02-14 02:33

    (This is pretty close to the boundary of a "question likely to elicit opinion" but believe it is an important issue, one for which you have offered code and data and useful citations, and so I hope there are no votes to close.)

    I admit that I have never really understood the S4 model of programming. However, what Chambers' post was saying is that @<-, i.e. slot assignment, was being re-implemented as a primitive rather than as a closure so that it would not require a complete copy of an object when one component was altered. So the earlier state of affairs will be altered in R 3.0.0 beta. On my machine (a 5 year-old MacPro running R 3.0.0 beta) the relative difference was even greater. However, I did not think that was necessarily a good test, since it was not altering an existing copy of a named object with multiple slots.

    res <-microbenchmark(structure(list(x=rep(1, 10^7)), class="MyS3Class"),
                    new("MyClass", x=rep(1, 10^7)) )
    summary(res)[ ,"median"]
    #[1] 145.0541 103.4064
    

    I think you should go with S4 since your brain structure is more flexible than mine and there are a lot of very smart people, Douglas Bates and Martin Maechler to name two other than John Chambers, who have used S4 methods for packages that require heavy processing. The Matrix and lme4 package both use S4 methods for critical functions.

提交回复
热议问题