It actually seems that formatC comes out faster:
library(microbenchmark) a <- round(runif(100000), 2) microbenchmark( as.character(a), formatC(a), format(a), sprintf('%.2f', a), gettextf('%.2f', a), paste0('', a) )
Conclusion:
Unit: milliseconds expr min lq mean median uq max neval as.character(a) 69.58868 70.74803 71.98464 71.41442 72.92168 82.21936 100 formatC(a) 33.35502 36.29623 38.83611 37.60454 39.27079 72.92176 100 format(a) 55.98344 56.78744 58.00442 57.64804 58.83614 66.15601 100 sprintf("%.2f", a) 46.54285 47.40126 48.53067 48.10791 49.12717 65.26819 100 gettextf("%.2f", a) 46.74888 47.81214 49.23166 48.60025 49.16692 84.90208 100 paste0("", a) 86.62459 88.67753 90.80720 89.86829 91.33774 125.51421 100
My sessionInfo :
R version 3.1.0 (2014-04-10) Platform: x86_64-apple-darwin13.1.0 (64-bit) locale: [1] en_AU.UTF-8/en_AU.UTF-8/en_AU.UTF-8/C/en_AU.UTF-8/en_AU.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] microbenchmark_1.4-2 loaded via a namespace (and not attached): [1] colorspace_1.2-4 digest_0.6.4 ggplot2_1.0.0 grid_3.1.0 gtable_0.1.2 MASS_7.3-35 [7] munsell_0.4.2 plyr_1.8.1 proto_0.3-10 Rcpp_0.11.3 reshape2_1.4 scales_0.2.4 [13] stringr_0.6.2 tools_3.1.0
source share