对于我的json编码器,我想打印一组带n小数的数字.两种方式是:
x <- c(1,2,pi)
n <- 2
format(x, digits = n, nsmall = n, trim = TRUE, drop0trailing = TRUE)
formatC(x, digits = n, format = "f", drop0trailing = TRUE)
Run Code Online (Sandbox Code Playgroud)
然而,该drop0trailing参数似乎引入了一个大的(~10x)性能回归:
x <- rnorm(1e6)
system.time(format(x, digits = n, nsmall = n, trim = TRUE))
user system elapsed
0.584 0.000 0.584
system.time(format(x, digits = n, nsmall = n, trim = TRUE, drop0trailing = TRUE))
user system elapsed
5.763 0.040 5.799
Run Code Online (Sandbox Code Playgroud)
是否有另一种方法可以打印带n小数的数字更快?
命令
as.character(round(x, n))
# [1] "1" "2" "3.14"
Run Code Online (Sandbox Code Playgroud)
应该快得多.该options(scipen = k)控件如果当它跳到科学记数法.
另一种选择是
sub("\\.0+$", "", sprintf(paste0("%.", n, "f"), x))
# [1] "1" "2" "3.14"
Run Code Online (Sandbox Code Playgroud)
该命令的优点是结果不是科学记数法.
绩效考核:
f1 <- function() format(x, digits = n, nsmall = n, trim = TRUE, drop0trailing = TRUE)
f2 <- function() formatC(x, digits = n, format = "f", drop0trailing = TRUE)
f3 <- function() as.character(round(x, n))
f4 <- function() sub("\\.0+$", "", sprintf(paste0("%.", n, "f"), x))
library(microbenchmark)
microbenchmark(f1(), f2(), f3(), f4())
# Unit: microseconds
# expr min lq median uq max neval
# f1() 288.594 294.6525 298.5165 302.5325 544.610 100
# f2() 319.022 324.4970 327.0815 331.4695 600.179 100
# f3() 9.799 12.4140 13.6315 13.9910 142.313 100
# f4() 40.198 42.6590 45.9945 46.6180 342.098 100
Run Code Online (Sandbox Code Playgroud)