Make this process more processor intensive and less memory intensive

。_饼干妹妹 提交于 2019-12-10 19:37:16

问题


This question is a follow-up to Count days per year.

I did what Dirk suggested with a huge data.frame. My commands look like this:

dateSeq <- function(df) {
  res <- seq(as.Date(df["begin"]), as.Date(df["end"]), by = "1 day")
  format(res, "%Y")
}

dataFrame$seq <- apply(dataFrame, 1, dateSeq)
dataFrame_years <- do.call("c", dataFrame[["seq"]])

rm(dataFrame)
gc()
gc()

dataFrame_tab <- table(dataFrame_years)

Now, these commands fill up my 8 GB Ram and 2 GB swap space. In the mean time my processor is bored having a processor load of maybe 15 %.

Besides, it takes ages for my computer to fulfill my "desires". Can I shift some of the work to the CPU and unburden my Ram a bit?


回答1:


Indeed, the referred solution is uneccessary memory hungry. Try this:

begin <- as.POSIXlt("2007-05-20", tz = "GMT")
end <- as.POSIXlt("2010-06-13", tz = "GMT")

year <- seq(begin$year, end$year) + 1900
year.begin <- as.POSIXlt(paste(year, "01", "01", sep="-"), tz="GMT")
year.begin[1] <- begin
year.end <- as.POSIXlt(paste(year, "12", "31", sep="-"), tz="GMT")
year.end[length(year.end)] <- end
days <- as.numeric(year.end - year.begin) + 1
cbind(year, days)


来源:https://stackoverflow.com/questions/9534351/make-this-process-more-processor-intensive-and-less-memory-intensive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!