English 中文(简体)
在 XTS 的不平等时间滚动列表
原标题:Rolling list over unequal times in XTS

我有盘点水平的盘点数据, 并希望为前 10 秒创建一份滚动列表, 列出所有盘点 。 下面的代码有效, 但大量数据需要很长的时间 。 我想将此进程向导化, 或者加快速度, 但我不会想出任何办法 。 任何建议或者正确方向的动作都会受到欢迎 。

library(quantmod)
set.seed(150)

# Create five minutes of xts example data at .1 second intervals
mins  <- 5
ticks <- mins * 60 * 10 + 1


times <- xts(runif(seq_len(ticks),1,100), order.by=seq(as.POSIXct("1973-03-17 09:00:00"),
                                                       as.POSIXct("1973-03-17 09:05:00"), length = ticks))

# Randomly remove some ticks to create unequal intervals
times <- times[runif(seq_along(times))>.3]

# Number of seconds to look back
lookback  <- 10
dist.list <- list(rep(NA, nrow(times)))

system.time(
  for (i in 1:length(times)) {

    dist.list[[i]] <- times[paste(strptime(index(times[i])-(lookback-1), format = "%Y-%m-%d %H:%M:%S"), "/",
                                  strptime(index(times[i])-1, format = "%Y-%m-%d %H:%M:%S"), sep = "")]
  }
)
>  user  system elapsed 
   6.12    0.00    5.85 
最佳回答

您应该检查 < code> window 函数, 它会让您的子日期选择更容易得多。 以下代码使用 < code> laply 来完成循环的工作 。

# Your code
system.time(
  for (i in 1:length(times)) {

    dist.list[[i]] <- times[paste(strptime(index(times[i])-(lookback-1), format = "%Y-%m-%d %H:%M:%S"), "/",
                                  strptime(index(times[i])-1, format = "%Y-%m-%d %H:%M:%S"), sep = "")]
  }
  )

#    user  system elapsed 
#    10.09    0.00   10.11

# My code 
system.time(dist.list<-lapply(index(times),
    function(x) window(times,start=x-lookback-1,end=x))
)
#    user  system elapsed 
#    3.02    0.00    3.03 

因此,大约三分之一更快。

但是,如果你真的想加快速度,而且你愿意放弃毫秒的准确性(我认为你最初的方法暗含了这一点),你只需用独特的日期-小时-秒组合来运行循环,因为它们都会同时返回同一时间窗口。这将加速大约二十或三十倍的事情:

dat.time=unique(as.POSIXct(as.character(index(times)))) # Cheesy method to drop the ms.
system.time(dist.list.2<-lapply(dat.time,function(x) window(times,start=x-lookback-1,end=x)))

# user  system elapsed 
# 0.37    0.00    0.39 
问题回答

暂无回答




相关问题
What to look for in performance analyzer in VS 2008

What to look for in performance analyzer in VS 2008 I am using VS Team system and got the performance wizard and reports going. What benchmarks/process do I use? There is a lot of stuff in the ...

SQL Table Size And Query Performance

We have a number of items coming in from a web service; each item containing an unknown number of properties. We are storing them in a database with the following Schema. Items - ItemID - ...

How to speed up Visual Studio 2008? Add more resources?

I m using Visual Studio 2008 (with the latest service pack) I also have ReSharper 4.5 installed. ReSharper Code analysis/ scan is turned off. OS: Windows 7 Enterprise Edition It takes me a long time ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

热门标签