R计算经过时间

R computation elapsed time

为了更好地理解,也许有人可以解释为什么我的 R 代码运行时间不是线性的:)

url <- c(NA)
id <- c(NA)
time <- c(NA)
j <- 1
l <- 1
id_p <- ""
for(i in 1:nrow(cookies_history)){
  if(i%%50000==0){
    print(i)
    print(Sys.time())
  }
  url_p <- substring(as.character(cookies_history$V3[i]), first=8) 
  url_p <- substring(url_p,first=1, last= regexpr("/",url_p)[1]-1)
  if(cookies_history$V1[i]!=id_p){
    id_p <- cookies_history$V1[i]
    id[j] <- cookies_history$V1[i]
    url[j] <- url_p
    time[j] <- cookies_history$V2[i]
    j <- j+1
    url_p2 <- url_p
  }else{
    if(url_p!=url_p2){
      id[j] <- cookies_history$V1[i]
      url[j] <- url_p
      time[j] <- cookies_history$V2[i]
      j <- j+1
      url_p2 <- url_p
    }
  }    
} 

这是 cookie 数据,其中 V1 是用户 ID,V2- 日期时间,V3- 完整 url。这里的函数打印结果:

50000
[1] "2016-01-19 19:42:28 EET"
[1] 100000
[1] "2016-01-19 19:42:58 EET"
[1] 150000
[1] "2016-01-19 19:43:31 EET"
[1] 200000
[1] "2016-01-19 19:44:23 EET"
[1] 250000
[1] "2016-01-19 19:45:20 EET"
[1] 300000
[1] "2016-01-19 19:46:24 EET"
[1] 350000
[1] "2016-01-19 19:47:37 EET"
[1] 400000
[1] "2016-01-19 19:48:53 EET"
[1] 450000
[1] "2016-01-19 19:51:00 EET"
[1] 500000
[1] "2016-01-19 19:53:22 EET"
[1] 550000
[1] "2016-01-19 19:56:18 EET"
[1] 600000
[1] "2016-01-19 19:58:50 EET"
[1] 650000
[1] "2016-01-19 20:02:04 EET"
[1] 700000
[1] "2016-01-19 20:05:14 EET"
[1] 750000
[1] "2016-01-19 20:09:17 EET"
[1] 800000
[1] "2016-01-19 20:13:14 EET"
[1] 850000
[1] "2016-01-19 20:17:18 EET"
[1] 900000
[1] "2016-01-19 20:21:59 EET"
[1] 950000
[1] "2016-01-19 20:26:33 EET"
[1] 1000000
[1] "2016-01-19 20:31:52 EET"
[1] 1050000
[1] "2016-01-19 20:36:50 EET"
[1] 1100000
[1] "2016-01-19 20:42:21 EET"
[1] 1150000
[1] "2016-01-19 20:47:33 EET"
[1] 1200000
[1] "2016-01-19 20:53:21 EET"
[1] 1250000
[1] "2016-01-19 20:59:49 EET"
[1] 1300000
[1] "2016-01-19 21:07:10 EET"
[1] 1350000
[1] "2016-01-19 21:16:30 EET"
[1] 1400000
[1] "2016-01-19 21:25:56 EET"
[1] 1450000
[1] "2016-01-19 21:34:50 EET"
[1] 1500000
[1] "2016-01-19 21:46:01 EET"
[1] 1550000

而且我想这种提取不值得用 R 来做? (因为我的研究需要提取 10 Gb 的 csv 数据文件) 样本:

structure(list(V1 = c(-2138197066L, -2138197066L, -2138197066L, 
-2138197066L, -2138197066L, -2138197066L, -2138197066L, -2138197066L, 
-2138197066L, -2138197066L), V2 = structure(c(8L, 9L, 10L, 7L, 
3L, 12L, 1L, 13L, 14L, 2L), .Label = c("2013-07-03 18:48:57", 
"2013-07-03 18:50:30", "2013-07-08 00:02:23", "2013-07-08 00:04:37", 
"2013-07-08 00:04:39", "2013-07-08 00:06:33", "2013-07-08 00:13:28", 
"2013-07-15 15:06:33", "2013-07-15 15:08:18", "2013-07-15 15:08:21", 
"2013-07-16 10:31:20", "2013-07-21 13:02:50", "2013-07-22 08:37:54", 
"2013-07-22 08:39:02", "2013-07-22 23:34:27", "2013-07-23 00:17:36", 
"2013-07-23 00:17:37", "2013-07-23 09:45:59", "2013-07-23 10:59:28"
), class = "factor"), V3 = structure(c(2L, 5L, 5L, 7L, 2L, 3L, 
2L, 8L, 11L, 4L), .Label = c("http://aka-cdn-ns.adtech.de/apps/415/Ad9253791St3Sz16Sq104537573V0Id1/iframe.html?adclick=http://adserver.adtech.de/adlink%7C323%7C4233738%7C0%7C16%7CAdId=9253791;BnId=1;itime=234786428;key=key1+key2+key3+key4;nodecode=yes;link=&adclickesc=http%3A//adserver.adtech.de/adlink%7C323%7C4233738%7C0%7C16%7CAdId%3D9253791%3BB", 
"http://ekstrabladet.dk/", "http://ekstrabladet.dk/biler/bil_anmeldelser/article2045233.ece", 
"http://ekstrabladet.dk/flash/dkkendte/article2030174.ece", "http://ekstrabladet.dk/flash/udlandkendte/article2038591.ece", 
"http://ekstrabladet.dk/flash/udlandkendte/article2047659.ece", 
"http://ekstrabladet.dk/musik/koncert_anmeldelser/article2034295.ece", 
"http://ekstrabladet.dk/nyheder/samfund/article2046966.ece", 
"http://ekstrabladet.dk/nyheder/samfund/article2048290.ece", 
"http://ekstrabladet.dk/sport/anden_sport/motorsport/formel_et/article2035091.ece", 
"http://ekstrabladet.dk/vrangen/article2046810.ece", "http://newz.dk/"
), class = "factor")), .Names = c("V1", "V2", "V3"), row.names = c(NA, 
10L), class = "data.frame")

如果不分析很难说,但正如 josilber 之前的评论所指出的,R 在 on-the-fly 大数据的增长方面苦苦挣扎。

Re-run,尝试对前三行使用以下替代方法。 Pre-allocate右边空格class:

url <- rep(as.character(NA),nrow(cookies_history))
id <- rep(as.integer(NA),nrow(cookies_history))
time <- rep(as.character(NA),nrow(cookies_history))

(我假设 url、id 和时间变量 classes;如果不是,请替换正确的 as.xxxx() 函数)。

没有必要 pre-allocate 每个url[i] 与虚拟数据,因为在引擎盖下,每个 url[i] 实际上是一个指向字符向量的指针;更改特定 url[i] 元素的内容不会修改 url 向量的整体结构,因此处理开销与 over-writing 单个变量没有区别。 (详见http://adv-r.had.co.nz/C-interface.html#c-data-structures)。

Re-sizing(通过添加一个元素)url(以及 id 和时间)向量定期涉及 re-building 整个向量。

tl;dr On-the-fly 构建或 re-sizing 向量很昂贵。