First we install mpoly and the rcpp version in development

install.packages("mpoly")
devtools::install_github("blaza/mpoly", ref = "bench")

Then we run some benchmarks. We’ll compare the speed of the core mpoly function and mpoly arithmetic, mainly multiplication and exponentiation.

# load test poly (untidy)
p1 <- list(
    c(coef = -4),
    c(coef = -4, x = 1),
    c(coef = 1, x = 1, y = 4),
    c(coef = -3, x = 1),
    c(coef = -1),
    c(coef = -4, y = 1),
    c(coef = -2, y = 1),
    c(coef = 3, y = 4, x = 1)
  )


# see if they're equal
print(mpoly::mpoly(p1))
## -7 x  +  4 x y^4  -  6 y  -  5
print(Rcppmpoly::mpoly(p1))
## -6 y  -  7 x  -  5  +  4 x y^4
# not exactly the same but equal
print(mpoly::mpoly(p1) == Rcppmpoly::mpoly(p1))
## [1] TRUE
require(microbenchmark)

microbenchmark(mpoly::mpoly(p1), Rcppmpoly::mpoly(p1), times = 1000)
## Unit: microseconds
##                  expr     min       lq      mean   median        uq
##      mpoly::mpoly(p1) 924.963 981.4490 1055.8700 1029.365 1077.7660
##  Rcppmpoly::mpoly(p1) 164.257 204.2685  254.5844  242.093  279.3945
##       max neval
##  4633.910  1000
##  3727.873  1000

So Rcpp version is quite faster in this simple case. Let’s try a significantly more complicated polynomial

# load test poly (untidy)
p1 <- mpoly::mp("x + y + 3 x y + 17 x^2  + 4y^3 x^2 + 15")^5
p2 <- mpoly::mp("-2x + 6y + 3 x^3 y + 17 y^2  + 4y^2 x^1 + 13")^5

p3 <- c(p1, p2)

# see if they're equal
print(mpoly::mpoly(p1) == Rcppmpoly::mpoly(p1))
## [1] TRUE
microbenchmark(mpoly::mpoly(p3), Rcppmpoly::mpoly(p3), times = 100)
## Unit: milliseconds
##                  expr       min        lq     mean   median        uq
##      mpoly::mpoly(p3) 21.228116 21.518442 22.95227 21.68941 24.592001
##  Rcppmpoly::mpoly(p3)  3.884242  3.977167  4.17752  4.02789  4.441114
##        max neval
##  37.199152   100
##   8.086575   100

So, again we have about 5 fold increase in speed.


I don’t know how to benchmark arithmetic, as they’re S3 functions and for some reason I can’t access them with e.g. mpoly::*.mpoly, but there is bound to be an increase also in arithmetic operations on mpolys when C++ is used for the bulk of the operations.