This evaluation is taken from the example section of mkinfit. When using an mkin version equal to or greater than 0.9-36 and a compiler (gcc) is installed, you will see a message that the model is being compiled from autogenerated C code when defining a model using mkinmod.
library("mkin")
SFO_SFO <- mkinmod(
parent = list(type = "SFO", to = "m1", sink = TRUE),
m1 = list(type = "SFO"))
## Compiling 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")
mb.1 <- microbenchmark(
mkinfit(SFO_SFO, FOCUS_2006_D, solution_type = "deSolve", use_compiled = FALSE,
quiet = TRUE),
mkinfit(SFO_SFO, FOCUS_2006_D, solution_type = "eigen", quiet = TRUE),
mkinfit(SFO_SFO, FOCUS_2006_D, solution_type = "deSolve", quiet = TRUE),
times = 3, control = list(warmup = 1))
smb.1 <- summary(mb.1)[-1]
rownames(smb.1) <- c("deSolve, not compiled", "Eigenvalue based", "deSolve, compiled")
print(smb.1)
## min lq mean median uq
## deSolve, not compiled 6980.8002 6996.4739 7024.5611 7012.1476 7046.4415
## Eigenvalue based 925.3350 928.9405 951.8405 932.5460 965.0932
## deSolve, compiled 747.2635 761.9405 771.4339 776.6174 783.5191
## max neval
## deSolve, not compiled 7080.7354 3
## Eigenvalue based 997.6404 3
## deSolve, compiled 790.4207 3
We see that using the compiled model is by a factor of 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:
smb.1["median"]/smb.1["deSolve, compiled", "median"]
## median
## deSolve, not compiled 9.029089
## Eigenvalue based 1.200779
## deSolve, compiled 1.000000
This evaluation is also taken from the example section of mkinfit.
FOMC_SFO <- mkinmod(
parent = list(type = "FOMC", to = "m1", sink = TRUE),
m1 = list(type = "SFO"))
## Compiling differential equation model from auto-generated C code...
mb.2 <- microbenchmark(
mkinfit(FOMC_SFO, FOCUS_2006_D, use_compiled = FALSE, quiet = TRUE),
mkinfit(FOMC_SFO, FOCUS_2006_D, quiet = TRUE),
times = 3, control = list(warmup = 1))
smb.2 <- summary(mb.2)[-1]
rownames(smb.2) <- c("deSolve, not compiled", "deSolve, compiled")
print(smb.2)
## min lq mean median uq
## deSolve, not compiled 14.127630 14.245064 14.298201 14.362497 14.383486
## deSolve, compiled 1.354744 1.362167 1.366362 1.369589 1.372171
## max neval
## deSolve, not compiled 14.404474 3
## deSolve, compiled 1.374752 3
smb.2["median"]/smb.2["deSolve, compiled", "median"]
## median
## deSolve, not compiled 10.48672
## deSolve, compiled 1.00000
Here we get a performance benefit of a factor of 10.5 using the version of the differential equation model compiled from C code using the inline package!