Mmtd 6.147010 6.177769 6.302549 6.208528 6.380318 6.552108 3Īnd to present a more complete picture, another test for character class (excluding Rfast::rowSort as it cannot handle character class): set.seed(1)Ī <- matrix(sample(letters, 9e6, TRUE),ncol=300) Timing code using same data as Craig: set.seed(1) There is also an equivalent for sorting by columns under comments in the same link. > a microbenchmark::microbenchmark(sorted identical(sorted,sorted2,sorted3)Īnother excellent method from Martin Morgan without any usage of external packages in Fastest way to select i-th highest value from row and assign to new column: matrix(a, ncol=ncol(a), byrow=TRUE) > a microbenchmark::microbenchmark(sorted set.seed(1) The package grr contains an alternate sort method that can be used to speed up this particular operation (I have reduced the matrix size somewhat so that this benchmark doesn't take forever) : > set.seed(1) System.time(sorted4 <- t(parApply(cl,a,1,sort.int, method='quick'))) # Forever. However, the overhead of transferring data seems to be too big, and on my machine it starts swapping since I "only" have 8 GB memory: library(parallel) System.time() # 26 secsīut a better way should be to use the parallel package to sort parts of the matrix in parallel. # using a for-loop is slightly faster than apply (and avoids transpose): Youre interested in what the smallest or largest key-value pair. System.time(sorted2 <- t(apply(a,1,sort.int, method='quick'))) # 27 secs You want to be able to get a range of entries on-demand. System.time(sorted <- t(apply(a,1,sort))) # 31 secs Still, you can eek some extra performance out of sort by directly calling sort.int and using method='quick': set.seed(1) ![]() Well, I'm not aware of that many ways to sort faster in R, and the problem is that you're only sorting 300 values, but many times.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |