【问题标题】:R's hist function running extremely slowly on large data setR 的 hist 函数在大型数据集上运行极其缓慢
【发布时间】:2020-10-06 20:31:42
【问题描述】:

我有一个包含 1,224,647 个 p 值的不太大(嗯,“大”是相对的)数组/向量。

在我的 Ubuntu VM 上运行大约需要 15 分钟。长时间运行让我很困扰,因为这是我必须分析的数据的一小部分。

我已经阅读了手册,运行了搜索引擎,我没有看到其他人有这个问题,这非常令人费解。

我的会话信息:

> sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.4 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] compiler_3.6.3

我的脚本很简单:

setEPS()
postscript('p_values.histogram.eps')
hist(d, breaks=10, main = 'p values', xlab = 'p')
dev.off()

我可以编写一个脚本来计算每个直方图 bin,然后制作一个简化的数据集,但这对于这样的例行任务来说似乎太复杂了。

真正奇怪的是当我跑步时

t0 <- proc.time()
source("tmp.R")
t1 <- proc.time()
print(t1-t0)
x100     <- rnorm(100)
x1000    <- rnorm(1000)
x10000   <- rnorm(10000)
x100000  <- rnorm(100000)
x1000000 <- rnorm(1000000)
#
t0 <- proc.time()
hist(x100)
t1 <- proc.time()
print("100")
print(t1-t0)
#-------------
t0 <- proc.time()
hist(x1000)
t1 <- proc.time()
print("1000")
print(t1-t0)
#-------------
t0 <- proc.time()
hist(x10000)
t1 <- proc.time()
print("10000")
print(t1-t0)
#-------------

t0 <- proc.time()
hist(x100000)
t1 <- proc.time()
print("100000")
print(t1-t0)
#-------------
t0 <- proc.time()
hist(x1000000)
t1 <- proc.time()
print("1000000")
print(t1-t0)
pdf("tmp.pdf")
#-------------
t0 <- proc.time()
hist(d)

t1 <- proc.time()
print("d")
dev.off()
print(t1-t0)

hist 函数运行得非常快……但它不在脚本中。

如何在 R 中以更合理的时间范围生成此直方图?有什么我不知道的技巧吗?

【问题讨论】:

  • 我认为它不应该改变任何东西,但是您是否尝试过使用其他设备,例如pdf?
  • @starja 不幸的是,运行 PDF 而不是 EPS 也非常慢。
  • 你能告诉我们str(d)吗?也许是一些奇怪的数据结构让hist()感到困惑?
  • @BenBolker 的输出 `str(d) num [1:1224647] 0.5498 0.423 0.4605 0.0107 0.9394 ...
  • 好吧,这是一个无聊的数字向量。我的测试功能在规模以下的表现如何?也许你的记忆力有限。 (尽管 1e7 的数字向量“仅”为 76 Mb)。您的环境中还有多少其他东西?

标签: r histogram


【解决方案1】:

在 Ubuntu 18.04(非虚拟,相当新的笔记本电脑)上,我的工作要好得多。

这是一个测试脚本,用于为n 均匀分布值的向量绘制直方图的动作计时:

mkhist <- function(n) {
    tt <- system.time({
        set.seed(101)
        x <- runif(n)
        setEPS()
        postscript("tmp.eps")
        hist(x,breaks=10)
        dev.off()
    })
    return(tt)
}

mkhist(1e6)
##   user  system elapsed 
##  0.049   0.016   0.065 

几乎可以线性扩展至 1 亿(5 秒):

mkhist(1e8)
##    user  system elapsed 
##   5.041   0.668   5.709

【讨论】:

  • 这个脚本也有同样的缓慢问题:(我是从一个文本文件运行的,这会有所不同吗?
  • 它如何扩展(例如,对于 n=1e3、1e4、1e5)?如果您拨打 EPS 电话会发生什么?
猜你喜欢
  • 2019-04-27
  • 2015-06-04
  • 2016-09-09
  • 1970-01-01
  • 2017-09-02
  • 2013-04-27
  • 1970-01-01
  • 2013-03-28
  • 2013-09-13
相关资源
最近更新 更多