From 1c7dbeec335c404377381157d34db6e219ce3f21 Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Fri, 5 May 2017 12:11:30 +0200 Subject: Switch from microbenchmark to rbenchmark --- DESCRIPTION | 6 +- vignettes/compiled_models.Rmd | 74 +++++++++++------------ vignettes/compiled_models.html | 132 ++++++++++++++++++----------------------- 3 files changed, 97 insertions(+), 115 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index cdc1a4a5..38e4e301 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Package: mkin Type: Package Title: Kinetic Evaluation of Chemical Degradation Data -Version: 0.9.45.1 -Date: 2016-12-21 +Version: 0.9.45.2 +Date: 2017-05-05 Authors@R: c(person("Johannes", "Ranke", role = c("aut", "cre", "cph"), email = "jranke@uni-bremen.de"), person("Katrin", "Lindenberger", role = "ctb"), @@ -18,7 +18,7 @@ Description: Calculation routines based on the FOCUS Kinetics Report (2006, purpose. Depends: minpack.lm, rootSolve, inline, parallel Imports: stats, graphics, methods, FME, deSolve, R6 -Suggests: knitr, testthat, microbenchmark, ggplot2, tikzDevice +Suggests: knitr, rbenchmark, tikzDevice, testthat License: GPL LazyLoad: yes LazyData: yes diff --git a/vignettes/compiled_models.Rmd b/vignettes/compiled_models.Rmd index 18e1a462..864cac87 100644 --- a/vignettes/compiled_models.Rmd +++ b/vignettes/compiled_models.Rmd @@ -41,60 +41,60 @@ SFO_SFO <- mkinmod( We can compare the performance of the Eigenvalue based solution against the compiled version and the R implementation of the differential equations using -the microbenchmark package. +the benchmark package. ```{r benchmark_SFO_SFO, fig.height = 3} -library("microbenchmark") -library("ggplot2") -mb.1 <- microbenchmark( - "deSolve, not compiled" = mkinfit(SFO_SFO, FOCUS_2006_D, - solution_type = "deSolve", - use_compiled = FALSE, quiet = TRUE), - "Eigenvalue based" = mkinfit(SFO_SFO, FOCUS_2006_D, - solution_type = "eigen", quiet = TRUE), - "deSolve, compiled" = mkinfit(SFO_SFO, FOCUS_2006_D, - solution_type = "deSolve", quiet = TRUE), - times = 3, control = list(warmup = 0)) - -smb.1 <- summary(mb.1) -print(mb.1) -autoplot(mb.1) +if (require(rbenchmark)) { + b.1 <- benchmark( + "deSolve, not compiled" = mkinfit(SFO_SFO, FOCUS_2006_D, + solution_type = "deSolve", + use_compiled = FALSE, quiet = TRUE), + "Eigenvalue based" = mkinfit(SFO_SFO, FOCUS_2006_D, + solution_type = "eigen", quiet = TRUE), + "deSolve, compiled" = mkinfit(SFO_SFO, FOCUS_2006_D, + solution_type = "deSolve", quiet = TRUE), + replications = 3) + print(b.1) + factor_SFO_SFO <- round(b.1["1", "relative"]) +} else { + factor_SFO_SFO <- NA + print("R package benchmark is not available") +} ``` -We see that using the compiled model is by a factor of -`r round(smb.1[1, "median"]/smb.1[3, "median"], 1)` +We see that using the compiled model is by a factor of around +`r factor_SFO_SFO` faster than using the R version with the default ode solver, and it is even faster than the Eigenvalue based solution implemented in R which does not need -iterative solution of the ODEs: +iterative solution of the ODEs. -```{r} -rownames(smb.1) <- smb.1$expr -smb.1["median"]/smb.1["deSolve, compiled", "median"] -``` ## Model that can not be solved with Eigenvalues This evaluation is also taken from the example section of mkinfit. ```{r benchmark_FOMC_SFO, fig.height = 3} -FOMC_SFO <- mkinmod( - parent = mkinsub("FOMC", "m1"), - m1 = mkinsub( "SFO")) - -mb.2 <- microbenchmark( - "deSolve, not compiled" = mkinfit(FOMC_SFO, FOCUS_2006_D, - use_compiled = FALSE, quiet = TRUE), - "deSolve, compiled" = mkinfit(FOMC_SFO, FOCUS_2006_D, quiet = TRUE), - times = 3, control = list(warmup = 0)) -smb.2 <- summary(mb.2) -print(mb.2) -smb.2["median"]/smb.2["deSolve, compiled", "median"] -autoplot(mb.2) +if (require(rbenchmark)) { + FOMC_SFO <- mkinmod( + parent = mkinsub("FOMC", "m1"), + m1 = mkinsub( "SFO")) + + b.2 <- benchmark( + "deSolve, not compiled" = mkinfit(FOMC_SFO, FOCUS_2006_D, + use_compiled = FALSE, quiet = TRUE), + "deSolve, compiled" = mkinfit(FOMC_SFO, FOCUS_2006_D, quiet = TRUE), + replications = 3) + print(b.2) + factor_FOMC_SFO <- round(b.2["1", "relative"]) +} else { + factor_FOMC_SFO <- NA + print("R package benchmark is not available") +} ``` Here we get a performance benefit of a factor of -`r round(smb.2[1, "median"]/smb.2[2, "median"], 1)` +`r factor_FOMC_SFO` using the version of the differential equation model compiled from C code! This vignette was built with mkin `r packageVersion("mkin")` on diff --git a/vignettes/compiled_models.html b/vignettes/compiled_models.html index c9501b11..53c5647f 100644 --- a/vignettes/compiled_models.html +++ b/vignettes/compiled_models.html @@ -4,14 +4,14 @@ - + - + Performance benefit by using compiled model definitions in mkin @@ -24,7 +24,7 @@ - + @@ -217,7 +217,7 @@ div.tocify {

Performance benefit by using compiled model definitions in mkin

Johannes Ranke

-

2016-11-18

+

2017-05-05

@@ -229,85 +229,67 @@ div.tocify {
##            gcc 
 ## "/usr/bin/gcc"

First, we build a simple degradation model for a parent compound with one metabolite.

-
library("mkin")
-
## Loading required package: minpack.lm
-
## Loading required package: rootSolve
-
## Loading required package: inline
-
## Loading required package: methods
-
## Loading required package: parallel
-
SFO_SFO <- mkinmod(
+
library("mkin")
+SFO_SFO <- mkinmod(
   parent = mkinsub("SFO", "m1"),
   m1 = mkinsub("SFO"))
## Successfully compiled differential equation model from auto-generated C code.
-

We can compare the performance of the Eigenvalue based solution against the compiled version and the R implementation of the differential equations using the microbenchmark package.

-
library("microbenchmark")
-library("ggplot2")
-mb.1 <- microbenchmark(
-  "deSolve, not compiled" = mkinfit(SFO_SFO, FOCUS_2006_D,
-                                    solution_type = "deSolve",
-                                    use_compiled = FALSE, quiet = TRUE),
-  "Eigenvalue based" = mkinfit(SFO_SFO, FOCUS_2006_D,
-                               solution_type = "eigen", quiet = TRUE),
-  "deSolve, compiled" = mkinfit(SFO_SFO, FOCUS_2006_D,
-                                solution_type = "deSolve", quiet = TRUE),
-  times = 3, control = list(warmup = 0))
-
## Warning in microbenchmark(`deSolve, not compiled` = mkinfit(SFO_SFO,
-## FOCUS_2006_D, : Could not measure overhead. Your clock might lack
-## precision.
-
smb.1 <- summary(mb.1)
-print(mb.1)
-
## Unit: milliseconds
-##                   expr       min        lq      mean    median        uq
-##  deSolve, not compiled 4919.3703 4922.1463 4957.6695 4924.9224 4976.8191
-##       Eigenvalue based  824.0317  840.7962  861.2648  857.5608  879.8813
-##      deSolve, compiled  691.8755  701.7453  706.2023  711.6151  713.3657
-##        max neval cld
-##  5028.7158     3   c
-##   902.2019     3  b 
-##   715.1164     3 a
-
autoplot(mb.1)
-

-

We see that using the compiled model is by a factor of 6.9 faster than using the R version with the default ode solver, and it is even faster than the Eigenvalue based solution implemented in R which does not need iterative solution of the ODEs:

-
rownames(smb.1) <- smb.1$expr
-smb.1["median"]/smb.1["deSolve, compiled", "median"]
-
##                         median
-## deSolve, not compiled 6.920767
-## Eigenvalue based      1.205091
-## deSolve, compiled     1.000000
+

We can compare the performance of the Eigenvalue based solution against the compiled version and the R implementation of the differential equations using the benchmark package.

+
if (require(rbenchmark)) {
+  b.1 <- benchmark(
+    "deSolve, not compiled" = mkinfit(SFO_SFO, FOCUS_2006_D,
+                                      solution_type = "deSolve",
+                                      use_compiled = FALSE, quiet = TRUE),
+    "Eigenvalue based" = mkinfit(SFO_SFO, FOCUS_2006_D,
+                                 solution_type = "eigen", quiet = TRUE),
+    "deSolve, compiled" = mkinfit(SFO_SFO, FOCUS_2006_D,
+                                  solution_type = "deSolve", quiet = TRUE),
+    replications = 3)
+  print(b.1)
+  factor_SFO_SFO <- round(b.1["1", "relative"])
+} else {
+  factor_SFO_SFO <- NA
+  print("R package benchmark is not available")
+}
+
##                    test replications elapsed relative user.self sys.self
+## 3     deSolve, compiled            3   2.742    1.000     2.740    0.000
+## 1 deSolve, not compiled            3  24.467    8.923    24.344    0.124
+## 2      Eigenvalue based            3   3.050    1.112     3.044    0.004
+##   user.child sys.child
+## 3          0         0
+## 1          0         0
+## 2          0         0
+

We see that using the compiled model is by a factor of around 9 faster than using the R version with the default ode solver, and it is even faster than the Eigenvalue based solution implemented in R which does not need iterative solution of the ODEs.

Model that can not be solved with Eigenvalues

This evaluation is also taken from the example section of mkinfit.

-
FOMC_SFO <- mkinmod(
-  parent = mkinsub("FOMC", "m1"),
-  m1 = mkinsub( "SFO"))
+
if (require(rbenchmark)) {
+  FOMC_SFO <- mkinmod(
+    parent = mkinsub("FOMC", "m1"),
+    m1 = mkinsub( "SFO"))
+
+  b.2 <- benchmark(
+    "deSolve, not compiled" = mkinfit(FOMC_SFO, FOCUS_2006_D,
+                                      use_compiled = FALSE, quiet = TRUE),
+    "deSolve, compiled" = mkinfit(FOMC_SFO, FOCUS_2006_D, quiet = TRUE),
+    replications = 3)
+  print(b.2)
+  factor_FOMC_SFO <- round(b.2["1", "relative"])
+} else {
+  factor_FOMC_SFO <- NA
+  print("R package benchmark is not available")
+}
## Successfully compiled differential equation model from auto-generated C code.
-
mb.2 <- microbenchmark(
-  "deSolve, not compiled" = mkinfit(FOMC_SFO, FOCUS_2006_D,
-                                    use_compiled = FALSE, quiet = TRUE),
-  "deSolve, compiled" = mkinfit(FOMC_SFO, FOCUS_2006_D, quiet = TRUE),
-  times = 3, control = list(warmup = 0))
-
## Warning in microbenchmark(`deSolve, not compiled` = mkinfit(FOMC_SFO,
-## FOCUS_2006_D, : Could not measure overhead. Your clock might lack
-## precision.
-
smb.2 <- summary(mb.2)
-print(mb.2)
-
## Unit: seconds
-##                   expr       min        lq      mean    median        uq
-##  deSolve, not compiled 10.667049 10.692450 10.719555 10.717851 10.745808
-##      deSolve, compiled  1.279783  1.280014  1.281444  1.280244  1.282274
-##        max neval cld
-##  10.773765     3   b
-##   1.284305     3  a
-
smb.2["median"]/smb.2["deSolve, compiled", "median"]
-
##   median
-## 1     NA
-## 2     NA
-
autoplot(mb.2)
-

-

Here we get a performance benefit of a factor of 8.4 using the version of the differential equation model compiled from C code!

-

This vignette was built with mkin 0.9.44.9000 on

-
## R version 3.3.2 (2016-10-31)
+
##                    test replications elapsed relative user.self sys.self
+## 2     deSolve, compiled            3   4.038    1.000     4.036    0.000
+## 1 deSolve, not compiled            3  55.209   13.672    54.888    0.324
+##   user.child sys.child
+## 2          0         0
+## 1          0         0
+

Here we get a performance benefit of a factor of 14 using the version of the differential equation model compiled from C code!

+

This vignette was built with mkin 0.9.45 on

+
## R version 3.4.0 (2017-04-21)
 ## Platform: x86_64-pc-linux-gnu (64-bit)
 ## Running under: Debian GNU/Linux 8 (jessie)
## CPU model: Intel(R) Core(TM) i7-4710MQ CPU @ 2.50GHz
-- cgit v1.2.1