R 中的a^2
和a^2L
之间是否存在差异?
速度差异?
精密?
到目前为止,我没有看到,只是想知道^ 2是作为log / exp对实现的,还是^ 2L作为乘法。如果a
不仅仅是一个向量呢?
更新
不,这不是重复,我知道2
和2L
之间的区别。问题是,这种差异是否会对电力运营商起作用?
答案 0 :(得分:6)
我的结论是 从未如此轻微更快当且仅当基数是整数时, on scalars
我的基准:
library(microbenchmark)
set.seed(1230)
num <- rnorm(1L)
int <- sample(100L, 1L)
microbenchmark(times = 100000L,
num^2L,
num^2,
int^2L,
int^2)
我机器上的时间安排:
# Unit: nanoseconds
# expr min lq mean median uq max neval
# num^2L 99 115 161.8495 121 166 11047 1e+05
# num^2 97 113 196.8615 119 165 3645369 1e+05
# int^2L 89 107 140.3745 111 120 3319 1e+05
# int^2 98 115 525.1727 120 166 34776551 1e+05
如果base或exponent是数字,则中位数时间基本相同(虽然num ^ num
可能有一个胖的上尾?)。
尽管integer ^ integer
在标量上有优势,但似乎(正如@ A.Webb在他自己的答案中所说明的那样),对于任何合理大小的向量,numeric ^ numeric
更快,并且对于相当常见的中等大小的矢量,它的速度要快得多。
500个基准的结果:
set.seed(1230)
ns <- as.integer(10^(seq(0, 6, length.out = 500L)))
mbs <- sapply(ns, function(n){
num = rnorm(n); int = as.integer(num)
summary(microbenchmark(times = 2000L, num ^ 2L, num ^ 2, int ^ 2L, int ^ 2),
unit = "relative")$median
})
这第一个情节得到了事物的要点。 n
表示numeric
,i
表示integer
。
最终,矢量大小的固定成本确实消耗了优势:
只有在可以忽略不计的长度i ^ i
最快:
绘图的要点是:
matplot(ns[ns < 5000], t(mbs[ , ns < 5000]),
type = "l", lty = 1L, lwd = 3L,
xlab = "length", ylab = "Relative Time",
main = "Through Length 5000",
col = c("black", "red", "green", "blue"))
legend("topleft", c("n ^ i", "n ^ n", "i ^ i", "i ^ n"),
col = c("black", "red", "green", "blue"),
lty = 1L, lwd = 3L)
答案 1 :(得分:4)
R在内部使用整数指数版本R_pow_di
,但^
operator仅调用R_pow
。话虽如此,matrix:
#########
# #
# #
# #
# #
#########
#include <stdio.h>
void read_file(const char *file_name);
size_t rows = 10;
size_t cols = 20;
int main(int argc, char *argv[]) {
//function call
}
void read_file(const char *file_name) {
FILE *myfile = fopen(file_name, "r");
int newRows = 0;
int newCols = 0;
char ch;
while(!feof(myfile)) {
ch = fgetc(file);
if(ch == '\n') {
newRows++;
} else {
newCols++;
}
}
rows = newRows;
cols = newCols;
}
特殊情况R_pow
为x^2
。因此,精度是相同的,但x*x
版本应该稍微慢一点,因为C级的长 - 双倍强制。这在以下基准中得到证明。
2L
这不会让你失眠。
链接转到源镜像。请注意,lngs<-rep(2L,1e6)
dbls<-rep(2.0,1e6)
n<-sample(100,1e6,replace=TRUE)
x<-rnorm(1e6)
microbenchmark(x^lngs,x^dbls,n^lngs,n^dbls)
# Unit: milliseconds
# expr min lq mean median uq max neval cld
# x^lngs 8.489547 9.804030 12.543227 11.719721 13.98702 19.92170 100 b
# x^dbls 5.622067 6.724312 9.432223 7.949713 10.89252 59.15342 100 a
# n^lngs 10.590587 13.857297 14.920559 14.200080 16.65519 19.55346 100 c
# n^dbls 8.331087 9.699143 12.414267 11.403211 14.20562 19.66389 100 b
,R_ADD
,R_SUB
和R_MUL
被定义为C宏以利用类型重载,但R_POW
被定义为内联{{ 1}}特殊情况然后调用R_DIV
(小写)。在x^2 = x*x
中再次检查该特殊情况将是重复的,除了在其他地方内部调用R_pow
。