- Dr Colin Gillespie
- Originally from the 1990 City of Culture (Glasgow)
- Senior Statistics Lecturer, Newcastle University
- Consultant at Jumping Rivers
- twitter: csgillespie
Slides: [jumpingrivers.com]
#> function (x, ...)
#> UseMethod("mean")
#> <bytecode: 0x73098b8>
#> <environment: namespace:base>
note the bytecode line
mean_r = function(x) {
total = 0
n = length(x)
for(i in 1:n)
total = total + x[i]/n
total
}
library("compiler")
cmp_mean_r = cmpfun(mean_r)
cmp_mean_r
#> function(x) {
#> total = 0
#> n = length(x)
#> for(i in 1:n)
#> total = total + x[i]/n
#> total
#> }
#> <bytecode: 0x5608fa8>
# Generate some data
x = rnorm(1000)
microbenchmark::microbenchmark(times = 10, unit = "ms", # milliseconds
mean_r(x), cmp_mean_r(x), mean(x))
#> Unit: milliseconds
#> expr min lq mean median uq max neval cld
#> mean_r(x) 0.358 0.361 0.370 0.363 0.367 0.43 10 c
#> cmp_mean_r(x) 0.050 0.051 0.052 0.051 0.051 0.07 10 b
#> mean(x) 0.005 0.005 0.008 0.007 0.008 0.03 10 a
There are a number of ways to complile code.
cmpfun()where \(N\) indices the level of optimisation (\(0\) to \(3\))
to the DESCRIPTION file
install.packages() are not compiled
R_COMPILE_PKGSR_COMPILE_PKGS=3 to ~/.Renviron## Windows users need Rtools
install.packages("ggplot2",
type = "source",
INSTALL_opts = "--byte-compile")
install.packages("benchmarkme")
benchmarkmelibrary("benchmarkme")
get_ram() #> 16.3 GB get_cpu() #> $vendor_id #> [1] "GenuineIntel" #> #> $model_name #> [1] "Intel(R) Core(TM) i7-4702HQ CPU @ 2.20GHz" #> #> $no_of_cores #> [1] 8
benchmarkmelibrary("benchmarkme")## On CRAN
## Tests based on a script by
## Simon Urbanek & Douglas Bates
res = benchmark_std(runs = 3)
benchmarkmelibrary("benchmarkme")
res = benchmark_std(runs = 2)
# # Programming benchmarks (5 tests):
# 3,500,000 Fibonacci numbers calculation (vector calc): 0.52 (sec).
# Grand common divisors of 1,000,000 pairs (recursion): 0.965 (sec).
# Creation of a 3500x3500 Hilbert matrix (matrix calc): 0.306 (sec).
# Creation of a 3000x3000 Toeplitz matrix (loops): 11.5 (sec).
# Escoufier's method on a 60x60 matrix (mixed): 1.17 (sec).
# # Matrix calculation benchmarks (5 tests):
# Creation, transp., deformation of a 5000x5000 matrix: 0.794 (sec).
# 2500x2500 normal distributed random matrix ^1000: 0.522 (sec).
# Sorting of 7,000,000 random values: 0.598 (sec).
# 2500x2500 cross-product matrix (b = a' * a): 6.56 (sec).
# Linear regr. over a 3000x3000 matrix (c = a \ b'): 4.5 (sec).
# # Matrix function benchmarks (5 tests):
benchmarkme# Upload results + # RAM, CPU, # OS, byte-compile, BLAS upload_results(res)
benchmarkmeplot(res)
benchmark_ioupload_results takes a five column matrix
system.time outputbenchmarkme releases@edinbR in the booking form