Rya*_*son 10 iteration performance r nested-loops
我正在使用R中的一个大整数向量(大约1000万个整数),我需要从这个向量中找到每个不同的整数对,它们相差500或更少,并得出它们之间差异的直方图(即对于每对,第二个减去第一个).
这是完全未实现的代码,可以非常缓慢地执行我想要的操作:
# Generate some random example data
V <- round(rnorm(100) * 1000)
# Prepare the histogram
my.hist <- rep(0, 500)
names(my.hist) <- as.character(seq(1,500))
for (x1 in V) {
for (x2 in V) {
difference = x2 - x1
if (difference > 0 && difference <= 500) {
my.hist[difference] = my.hist[difference] + 1
}
}
}
Run Code Online (Sandbox Code Playgroud)
(假设每个整数都是唯一的,因此该difference > 0位是正确的.这是允许的,因为我实际上并不关心差异为零的任何情况.)
这是一些矢量化内循环的代码:
my.hist2 <- rep(0, 500)
names(my.hist2) <- as.character(seq(1,500))
for (x1 in V) {
differences <- V[V > x1 & V <= x1+500] - x1
difftable <- table(differences)
my.hist2[names(difftable)] = my.hist2[names(difftable)] + difftable
}
Run Code Online (Sandbox Code Playgroud)
这肯定比第一个版本更快.然而,即使这个变体在V包含仅500000个元素(50万个)时已经太慢了.
我可以这样做,没有任何显式循环如下:
X <- combn(V, 2)
# X is a matrix with two rows where each column represents a pair
diffs <- abs(X[2,] - X[1,])
my.hist3 <- table(diffs[diffs <= 500])
Run Code Online (Sandbox Code Playgroud)
但是,矩阵X将包含10e6 * (10e6 - 1) / 2或约50,000,000,000,000列,这可能是一个问题.
那么有没有办法在没有显式循环(太慢)或扩展所有对的矩阵(太大)的情况下做到这一点?
如果你想知道为什么我需要这样做,我正在实现这个:http://biowhat.ucsd.edu/homer/chipseq/qc.html#Sequencing_Fragment_Length_Estimation
Vin*_*ynd 16
一种可能的改进是对数据进行排序:距离小于500的对(i,j)将接近对角线,并且您不必探索所有值.
代码看起来像这样(它仍然很慢).
n <- 1e5
k <- 500
V <- round(rnorm(n) * n * 10)
V <- as.integer(V)
V <- sort(V)
h <- rep(0,k)
for(i in 1:(n-1)) {
for(j in (i+1):n) {
d <- V[j] - V[i]
if( d > k ) break
if( d > 0 ) h[d] <- h[d]+1
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:如果你想要更快的东西,你可以用C编写循环.你的1000万元素需要1秒.
n <- 10e6
k <- 500
V <- round(rnorm(n) * n * 10)
V <- as.integer(V)
V <- sort(V)
h <- rep(0,k)
library(inline)
sig <- signature(n="integer", v="integer", k="integer", h="integer")
code <- "
for( int i = 0; i < (*n) - 1; i++ ) {
for( int j = i + 1; j < *n; j++ ) {
int d = v[j] - v[i];
if( d > *k ) break;
if( d > 0 ) h[d-1]++;
}
}
"
f <- cfunction( sig, code, convention=".C" )
h <- f(n,V,k,h)$h
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
892 次 |
| 最近记录: |