All is in the title. I would expect that order
uses sort
to find the order of the values in a vector. Thus sort
should be quicker than order
to sort a vector, but this is not the case:
library(microbenchmark)
ss=sample(100,10000,replace=T)
microbenchmark(sort(ss))
microbenchmark(ss[order(ss)])
result:
> microbenchmark(sort(ss))
Unit: microseconds
expr min lq mean median uq max neval
sort(ss) 141.535 144.6415 173.6581 146.358 150.2295 2531.762 100
> microbenchmark(ss[order(ss)])
Unit: microseconds
expr min lq mean median uq max neval
ss[order(ss)] 109.198 110.9865 115.6275 111.901 115.3655 197.204 100
Example with a larger vector:
ss=sample(100,1e8,replace=T)
microbenchmark(sort(ss), ss[order(ss)], times = 5)
# Unit: seconds
# expr min lq mean median uq max neval
# sort(ss) 5.427966 5.431971 5.892629 6.049515 6.207060 6.346633 5
# ss[order(ss)] 3.381253 3.500134 3.562048 3.518079 3.625778 3.784997 5
sort
keep being aroun 1.6 times slower thatorder
. But as suggested by @Dan Hall in his answer, this ratio shrink to 1.15 when using directlysort.int
. – Vivienviviene