As daroczig said, you have an additional system.time. But there is something else:
If you put browser() in your function, you will see what happens. In fact, the expression you make is evaluated only once and then stored in memory. This is how R optimizes internally. So if you do this:
system.time.summary(N,(1:1e8)^2 +1)
t.mat internally:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] user.self 0.61 0 0 0 0 0 0 0 0 0 sys.self 0.36 0 0 0 0 0 0 0 0 0 elapsed 0.97 0 0 0 0 0 0 0 0 0 user.child NA NA NA NA NA NA NA NA NA NA sys.child NA NA NA NA NA NA NA NA NA NA
and expr:
Browse[2]> str(expr) num [1:100000000] 2 5 10 17 26 37 50 65 82 101 ...
This is a bit difficult to change, since R will evaluate any static expression only once, and then retrieve the result another 99 times from memory. If you want this not to happen, you must explicitly pass the expression and add the eval() function.
system.time.summary <- function(N, expr) { t.mat <- replicate(N, system.time(eval(expr))) as.data.frame(apply(t.mat[1:3,], 1, summary)) } system.time.summary(N, expression((1:1e8)^2 + 1))
Now expr gets evaluated every time and remains an expression in the function:
Browse[2]> expr expression((1:1e+08)^2 + 1)
This gives the correct timings.
user.self sys.self elapsed Min. 0.6400 0.2000 0.970 1st Qu. 0.6850 0.2375 0.980 Median 0.7150 0.2700 0.985 Mean 0.7130 0.2700 0.985 3rd Qu. 0.7425 0.2975 0.990 Max. 0.7800 0.3500 1.000