# [Rcpp-devel] Indexing vector with x(i) and x[i] gives remarkable difference in computing time

Romain Francois romain at r-enthusiasts.com
Thu Nov 27 13:34:03 CET 2014

```> Le 27 nov. 2014 à 13:22, Søren Højsgaard <sorenh at math.aau.dk> a écrit :
>
> Dear all,
> By "accident" I noticed that indexing with x(i) and x[i] gives remarkable difference in computing time. For example:
>
> library(Rcpp)
> cppFunction('
> IntegerVector insert1 (int n){
>  IntegerVector z(n);
>  for (int i=0; i<n; ++i) z(i) = i;  // NOTICE: using ()
>  return z;
> }')
> cppFunction('
> IntegerVector insert2 (int n){
>  IntegerVector z(n);
>  for (int i=0; i<n; ++i) z[i] = i;  // NOTICE: using []
>  return z;
> }')
>
> Both functions "work":
> n <- 10; insert1(n); insert2(n)
> [1] 0 1 2 3 4 5 6 7 8 9
> [1] 0 1 2 3 4 5 6 7 8 9
>
> But notice this difference in computing time:
> n <- 50000
> microbenchmark::microbenchmark( insert1(n), insert2(n) )
> Unit: microseconds
>       expr     min       lq     mean   median       uq      max neval cld
> insert1(n) 391.154 406.5500 416.9694 407.5005 421.1855  731.752   100   b
> insert2(n) 149.771 156.8045 189.9401 157.3745 163.0760 1204.635   100  a
>
> So using z(i) is more than twice as slow as using z[i].
>
> This prompts me to ask:
> 1) Why is that?

bounds checking. operator()(int i) checks that is i is between 0 and the length of the vector.

> 2) In the vignettes it is always [i] that are used for vectors. But why is it that (i,j) is then used for matrices. Wouldn't it be preferable if one could use both [] and () for indexing in both cases (and with the same speed) - or are there deeper reasons for this?

No such thing as a operator[](int, int) in C++ so we had to use ()

>
> Best regards
> Søren
>
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

```