解释Rprofile输出:这是什么<;匿名>;功能?

解释Rprofile输出:这是什么<;匿名>;功能?,r,profiling,anonymous-function,R,Profiling,Anonymous Function,所以我有一个运行MCMC算法的大函数。我相信大多数 昂贵的运算是大型矩阵的乘法,但这种Rprof输出相当复杂 $by.self self.time self.pct total.time total.pct "<Anonymous>" 328.90 81.84 329.34 81.95 "fprod" 46.16 11.49 376.02 93.57 "Dikin_Walk"

所以我有一个运行MCMC算法的大函数。我相信大多数 昂贵的运算是大型矩阵的乘法,但这种Rprof输出相当复杂

$by.self
                self.time self.pct total.time total.pct
"<Anonymous>"      328.90    81.84     329.34     81.95
"fprod"             46.16    11.49     376.02     93.57
"Dikin_Walk"         7.42     1.85     401.32     99.86
"as.vector"          5.98     1.49      57.56     14.32
".External"          2.54     0.63       2.54      0.63
"-"                  1.84     0.46       1.84      0.46
"H_x"                1.16     0.29     225.82     56.19
"fcrossprod"         1.14     0.28     226.12     56.27
这是总的
by.total
输出:

$by.total
                      total.time total.pct self.time self.pct
"Dikin_Walk"              401.32     99.86      7.42     1.85
"fprod"                   376.02     93.57     46.16    11.49
"<Anonymous>"             329.34     81.95    328.90    81.84
"cbind"                   268.58     66.83      0.04     0.01
"fcrossprod"              226.12     56.27      1.14     0.28
"H_x"                     225.82     56.19      1.16     0.29
"fsolve"                  203.82     50.72      0.14     0.03
"ellipsoid"               126.30     31.43      0.56     0.14
"fdet"                     64.84     16.13      0.02     0.00
"as.vector"                57.56     14.32      5.98     1.49
"fdiag"                    35.68      8.88      0.50     0.12
指的是匿名(未命名)函数。如果在循环中运行这样一个函数,那么大部分时间通常都会花在这个函数上

显然,
A_b
是一个矩阵,
x
是一个向量。使用矩阵代数而不是循环:

A_b <- matrix(1:16, 4)
x <- 1:3
D <- apply(A_b, 1, function(row) {1 / (row[1] - sum(row[-1] * x))})

D1 <- as.vector(1/(A_b[,1] - A_b[,-1] %*% x))

identical (D, D1)
#[1] TRUE
你的大部分时间都花在矩阵乘法上。您可能会从优化的BLAS中受益,例如,您可以尝试OpenBLAS。

首先,忽略“自我时间”,因为“总时间”包括该时间加上被调用方。 如果您花费了任何不需要的时间,那么您很可能通过调用函数而不是通过处理来完成**

第二,不要看那个。 Rprofile生成堆栈跟踪文件。 看看其中几个,随机选择的。 如果一个函数占80%的时间,您将在大约5个堆栈跟踪中的4个上看到它。 更重要的是,你会看到是谁在呼叫它,你会看到它在呼叫谁,从而使时间被花掉。 简单的数字并不能告诉你这一点。 对堆栈跟踪进行排序也不会告诉您这一点

如果它能给出打电话的电话号码就更好了,但它没有。 即使如此,仅仅显示函数仍然非常有用


**剖析者只显示“自我时间”,因为他们总是这样,因为所有其他人都这样做,很少有人意识到这只是一种分心。如果函数位于堆栈跟踪的终点,则它处于“自时间”。无论哪种方式,它都在“包含时间”中。

您的问题是,这是指被称为的函数,还是为什么该函数会占用这么多时间?我想这只是说匿名,因为您没有命名该函数。@Dason抱歉,我想我的问题是,为什么这个匿名函数会占用这么长时间。我的代码中没有其他匿名函数。@Roland
rowSums
计算每行的和,这样代码就不会计算A的第i行与x的点积了?我在代码中没有看到匿名函数。它必须在其他函数中调用。看看“by.total”输出。我切换了它,但我认为我实际上对函数的解释是错误的:
$by.self.time-self.pct-total.time-total.pct”“122.94 96.97 122.94 96.97
你有什么想法吗?没有看到更多的代码。我认为您所展示的可能实际上是分析输出中的“乐趣”,因为这是函数传递到的
apply
的参数。代码中的其他地方必须有匿名函数。因为我没有那个代码,我不能帮你找到它。非常感谢。我现在就发布一个编辑,你介意看一下吗?我明白了。很高兴知道大部分时间都花在了应该花的地方。非常感谢你的帮助!
$by.total
                      total.time total.pct self.time self.pct
"Dikin_Walk"              401.32     99.86      7.42     1.85
"fprod"                   376.02     93.57     46.16    11.49
"<Anonymous>"             329.34     81.95    328.90    81.84
"cbind"                   268.58     66.83      0.04     0.01
"fcrossprod"              226.12     56.27      1.14     0.28
"H_x"                     225.82     56.19      1.16     0.29
"fsolve"                  203.82     50.72      0.14     0.03
"ellipsoid"               126.30     31.43      0.56     0.14
"fdet"                     64.84     16.13      0.02     0.00
"as.vector"                57.56     14.32      5.98     1.49
"fdiag"                    35.68      8.88      0.50     0.12
prodCpp <- 'typedef Eigen::Map<Eigen::MatrixXd> MapMatd;
const MapMatd B(as<MapMatd>(BB));
const MapMatd C(as<MapMatd>(CC));
return wrap(B * C);'

fprod <- cxxfunction(signature(BB = "matrix", CC = "matrix"),
                      prodCpp, "RcppEigen")
A_b <- matrix(1:16, 4)
x <- 1:3
D <- apply(A_b, 1, function(row) {1 / (row[1] - sum(row[-1] * x))})

D1 <- as.vector(1/(A_b[,1] - A_b[,-1] %*% x))

identical (D, D1)
#[1] TRUE
B <- matrix(rnorm(1e6),1e3)
C <- matrix(rnorm(1e6),1e3)

Rprof()
for (i in 1:30) BC <- fprod(B, C)
Rprof(NULL)
summaryRprof()
#$by.self
#              self.time self.pct total.time total.pct
#"<Anonymous>"      4.24      100       4.24       100
#
#$by.total
#              total.time total.pct self.time self.pct
#"<Anonymous>"       4.24       100      4.24      100
#"fprod"             4.24       100      0.00        0
#
#$sample.interval
#[1] 0.02
#
#$sampling.time
#[1] 4.24