I'm not an R user but am trying to generate some benchmark info on various computers I administer to inform an upcoming purchase.
I'm using R on the command line (versiona 3.2.3), and typing the following inside R, but this does not generate any results within R. Note the rbenchmark package has already been installed.
Any suggestions or ideas will be much appreciated! Thanks!
> source("rbenchmark_ex.R")
Loading required package: rbenchmark
>
The rbenchmark_ex.R file:
require('rbenchmark')
benchmark(1:10^6)
# Example 1 ------
# Benchmarking the allocation of one 10^6-element numeric vector,
# by default replicated 100 times
benchmark(1:10^6)
# simple test functions used in subsequent examples
random.array = function(rows, cols, dist=rnorm)
array(dist(rows*cols), c(rows, cols))
random.replicate = function(rows, cols, dist=rnorm)
replicate(cols, dist(rows))
# Example 2 ----------
# Benchmarking an expression multiple times with the same replication count,
# output with selected columns only
benchmark(replications=rep(100, 3),
random.array(100, 100),
random.array(100, 100),
columns=c('test', 'elapsed', 'replications'))
# Example 3 ---------
# Benchmarking two named expressions with three different replication
# counts, output sorted by test name and replication count,
# with additional column added after the benchmark
within(benchmark(rep=random.replicate(100, 100),
arr=random.array(100, 100),
replications=10^(1:3),
columns=c('test', 'replications', 'elapsed'),
order=c('test', 'replications')),
{ average = elapsed/replications })
# Example 4
# Benchmarking a list of arbitrary predefined expressions
tests = list(rep=expression(random.replicate(100, 100)),
arr=expression(random.array(100, 100)))
do.call(benchmark,
c(tests, list(replications=100,
columns=c('test', 'elapsed', 'replications'),
order='elapsed')))
If you source a file, nothing is printed by default. There are several approaches to solve this, depending on what exactly you want.
If you want to force that just a few things are printed, you can wrap these in the print() command. Example:
print(benchmark(1:10^6))
If you want to print everything, then you can also supply the source() function with further arguments. There are three useful possibilities:
source("rbenchmark_ex.R", echo = TRUE): This echoes the code that is evaluated and prints the result after each line of code. Basically, this looks as if you would type each line from the script to the console and evaluate it.
source("rbenchmark_ex.R", print.eval = TRUE): This only prints the result, but does not echo the source code.
source("rbenchmark_ex.R", echo = TRUE, print.eval = FALSE): This only echoes the code, but does not print the results. (maybe not so useful...)
Maybe you'd rather have the outputs in a file. This can be done by using sink() (as suggested by 42- in his comment). Just add the following line to your script and all output that comes afterwards will be written to the file:
sink("output.txt")
You can then source the script in the R console, still using the options described above to specify, which kind of output you want.
You can also do this directly from the command line without having to start the R console first. For example:
Rscript -e 'source("rbenchmark_ex.R", echo = TRUE)'
This will write the output to the console (from where you can, of course, redirect it with >) or to a file, if you have used sink() in the script. You could also directly run
Rscript rbenchmark_ex.R
but this will only print result and not echo the code.