21.3 Measuring performance, Profiling

Two options

Here are two examples using the above for loop. First, we’ll use profvis.

Code
library(profvis)
pv = profvis({
  start.time1 = Sys.time()
  set.seed(1)
  N = 100000000
  J = 3
  df1 = NULL
  
  for(j in 1:J){
    
    cat(j, '')
    print(Sys.time())
    
    temp = data.frame(x = rnorm(N), 
                      j = j)
    df1 = rbind(df1, temp)
    
  }
  
  final.time1 = Sys.time()
  print(final.time1)
})

pv

Now we’ll use lineprof.

Code
library(lineprof)
lp = lineprof({
  start.time1 = Sys.time()
  set.seed(1)
  N = 100000000
  J = 3
  df1 = NULL
  
  for(j in 1:J){
    
    cat(j, '')
    print(Sys.time())
    
    temp = data.frame(x = rnorm(N), 
                      j = j)
    df1 = rbind(df1, temp)
    
  }
  
  final.time1 = Sys.time()
  print(final.time1)
})

shine(lp)

The output of profvis seems to be more useful, at least for this example for loop. The lineprof has three separate entries for rnorm and rbind. Probably because this expects a function, not a for loop.

Let’s see what microbenchmark does.

Code
microbenchmark::microbenchmark({
  
  
})
Unit: nanoseconds
 expr min lq  mean median uq  max neval
  { }  10 20 31.98     20 20 1042   100