您仍然可以从速度提升中获益,比如(假设为数字数据):
-sort(-x, partial = 1:5)[1:5]
标杆管理:
set.seed(3)
x <- sample(1:100000, 500000, replace = TRUE)
bench::mark(
snoram = -sort(-x, partial = 1:5)[1:5],
OP = sort(x, decreasing = TRUE)[1:5],
sotos_check = x[order(x, decreasing = TRUE)][1:5],
jogo = {p <- length(x) - 0:4; sort(x, partial = p)[p]}
)
# A tibble: 4 x 14
expression min mean median max `itr/sec` mem_alloc n_gc n_itr total_time result memory time gc
<chr> <bch:tm> <bch:tm> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl> <int> <bch:tm> <list> <list> <list> <list>
1 snoram 6.87ms 7.77ms 7.43ms 15.04ms 129. 5.72MB 9 34 264ms <int [5]> <Rprofmem [3 x 3]> <bch:tm> <tibble [43 x 3]>
2 OP 17.4ms 18.96ms 18.56ms 24.37ms 52.7 3.81MB 3 21 398ms <int [5]> <Rprofmem [2 x 3]> <bch:tm> <tibble [24 x 3]>
3 sotos_check 14.65ms 17.07ms 16.48ms 25.58ms 58.6 3.81MB 4 23 393ms <int [5]> <Rprofmem [2 x 3]> <bch:tm> <tibble [27 x 3]>
4 jogo 4.98ms 5.45ms 5.35ms 8.91ms 184. 3.81MB 6 37 201ms <int [5]> <Rprofmem [2 x 3]> <bch:tm> <tibble [43 x 3]>