Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

183
Visualizações
R benchmark code not generating output

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')))
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

If you source a file, nothing is printed by default. There are several approaches to solve this, depending on what exactly you want.

Output in the R console

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...)

Output to a file

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.

Using the command line

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.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda