system.time() returns CPU (and other) times that expr used.

x <- rep(NA,10000)
system.time(for(i in 1:10000) x[i] <- mean(runif(1000)))  
##    user  system elapsed 
##    0.56    0.00    0.56
system.time(sort(x))  
##    user  system elapsed 
##       0       0       0

microbenchmark serves as a more accurate replacement of the often seen system.time(replicate(1000, expr)) expression. It tries hard to accurately measure only the time it takes to evaluate expr. To achieved this, the sub-millisecond (supposedly nanosecond) accurate timing functions most modern operating systems provide are used. Additionally all evaluations of the expressions are done in C code to minimze any overhead.

library(microbenchmark)
m <- microbenchmark(
  xs <- 1:1000,
  for(i in 1:1000) {xs[i] <- i},
  xs <- rnorm(1000),
  times = 250
)
m
## Unit: nanoseconds
##                                  expr     min      lq        mean  median
##                          xs <- 1:1000       0     855    1254.432     856
##  for (i in 1:1000) {     xs[i] <- i } 1172132 1230289 1342391.128 1264927
##                     xs <- rnorm(1000)   83388   85954   93707.820   89375
##       uq     max neval
##     1284   17533   250
##  1323940 3895700   250
##    94935  244604   250
library(ggplot2) 
qplot(y=time, data=m, colour=expr) + scale_y_log10()

The library consists of just one function, benchmark, which is a simple wrapper around system.time.

Given a specification of the benchmarking process (counts of replications, evaluation environment) and an arbitrary number of expressions, benchmark() evaluates each of the expressions in the specified environment, replicating the evaluation as many times as specified, and returning the results conveniently wrapped into a data frame.

library(rbenchmark)

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

results <- 
  benchmark(random.array(100, 100),
            random.replicate(100, 100),
            columns=c('test', 'elapsed', 'replications'),
            replications=c(20,40),
            order="elapsed")

results[results$test == "random.array(100, 100)",]
##                     test elapsed replications
## 1 random.array(100, 100)    0.03           20
## 3 random.array(100, 100)    0.04           40
results[results$test == "random.replicate(100, 100)",]
##                         test elapsed replications
## 2 random.replicate(100, 100)    0.04           20
## 4 random.replicate(100, 100)    0.05           40