简介
我有一个函数将日期作为输入,在一段时间内进行一些计算 - 由Sys.sleep()
表示 - 删除日期中的所有'-'
并返回一个字符:
library(maggritr)
auxialiaryCompute = function(vDate)
{
Sys.sleep(1)
vDate %>% as.character %>% gsub("-", "", .)
}
> auxialiaryCompute(as.Date("2015-01-14"))
[1] "20150114"
冷却。上面的输出是'20150114'
。现在我想在此函数中包含先前的输出。或前两天,或n
之前的输出,直到过去的有限日期loopBackMaxDate
。
粗略递归
这是一个可能的递归代码:
compute = function(vDate, loopBackMaxDate=vDate, loopBackDays=0)
{
d = as.Date # short alias
dates = Filter(function(x) x>d(loopBackMaxDate),
getPreviousDates(loopBackDays, d(vDate)))
if(length(dates)==0)
return(auxialiaryCompute(vDate=vDate, previousOutputs=list()))
previousOutputs = lapply(dates, function(u) compute(u, loopBackMaxDate, loopBackDays))
auxialiaryCompute(vDate=vDate, previousOutputs=previousOutputs)
}
auxialiaryCompute = function(vDate, previousOutputs=list())
{
Sys.sleep(1)
vDate %>% as.character %>% gsub("-", "", .)
}
getPreviousDates = function(loopBackDays, vDate)
{
if(loopBackDays==0) return()
seq.Date(from=vDate-loopBackDays, to=vDate-1, by="days")
}
有了这个,我有与以前相同的结果(平均花费1秒):
> compute(as.Date("2015-01-14"))
[1] "20150114"
以下内容有效4
秒:
> system.time(compute("2014-05-05", loopBackMaxDate="2014-05-01", loopBackDays=1))
user system elapsed
0.00 0.00 3.99
我想计算下面的内容,需要3秒钟:
> system.time(compute("2014-05-04", loopBackMaxDate="2014-05-01", loopBackDays=1))
user system elapsed
0.02 0.00 3.01
这非常糟糕,因为我再次计算vDate="2014-05-04"
,vDate="2014-05-03"
和vDate="2014-05-02"
的结果,而在调用compute("2014-05-05", loopBackMaxDate="2014-05-01", loopBackDays=1)
时已完成...
记事递归
以下是我通过memoized进行的操作:
library(memoise)
compute = memoise(function(vDate, loopBackMaxDate=vDate, loopBackDays=0)
{
d = as.Date # short alias
dates = Filter(function(x) x>d(loopBackMaxDate), getPreviousDates(loopBackDays, d(vDate)))
if(length(dates)==0)
return(auxialiaryCompute(vDate=vDate, previousOutputs=list()))
previousOutputs = lapply(dates, function(u) compute(u, loopBackMaxDate, loopBackDays))
auxialiaryCompute(vDate=vDate, previousOutputs=previousOutputs)
})
auxialiaryCompute = memoise(function(vDate, previousOutputs=list())
{
Sys.sleep(1)
vDate %>% as.character %>% gsub("-", "", .)
})
首次运行(有效4秒):
> system.time(compute("2014-05-05", loopBackMaxDate="2014-05-01", loopBackDays=1))
user system elapsed
0.00 0.00 4.01
第二次运行需要1秒,而我预计需要0秒:
> system.time(compute("2014-05-04", loopBackMaxDate="2014-05-01", loopBackDays=1))
user system elapsed
0.00 0.00 0.99
我认为我在某处完全错了...我可以将输出存储在全局变量中,但我真的希望它能够使用memoization或连续样式传递并避免冗余计算!
如果有人有想法,我将非常感激!
答案 0 :(得分:0)
好的,首先,我在auxiliaryCompute
函数上添加了一些loginfo:
compute = memoise(function(vDate, loopBackMaxDate=vDate, loopBackDays=0)
{
d = as.Date # short alias
dates = Filter(function(x) x>d(loopBackMaxDate), getPreviousDates(loopBackDays, d(vDate)))
if(length(dates)==0)
{
loginfo("I reached the tail!")
return(auxiliaryCompute(vDate=vDate, previousOutputs=0))
}
previousOutputs = lapply(dates, function(u){
compute(vDate=u, loopBackMaxDate=loopBackMaxDate, loopBackDays)
})
auxiliaryCompute(vDate2=vDate, previousOutputs=previousOutputs)
})
auxiliaryCompute = memoise(function(vDate2, previousOutputs)
{
loginfo("-------arguments in auxiliaryCompute are: vDate %s , previousOutputs %s", vDate2, unlist(previousOutputs))
# Sys.sleep(1)
vDate2 %>% as.character %>% gsub("-", "", .)
})
> compute("2015-01-10", "2015-01-01", 2)
2015-01-20 18:53:12 INFO::I reached the tail!
2015-01-20 18:53:12 INFO::-------arguments: vDate 2015-01-02 , previousOutputs 0
2015-01-20 18:53:12 INFO::-------arguments: vDate 2015-01-03 , previousOutputs 20150102
2015-01-20 18:53:12 INFO::-------arguments: vDate 2015-01-04 , previousOutputs 20150102,20150103
2015-01-20 18:53:12 INFO::-------arguments: vDate 2015-01-05 , previousOutputs 20150103,20150104
2015-01-20 18:53:12 INFO::-------arguments: vDate 2015-01-06 , previousOutputs 20150104,20150105
2015-01-20 18:53:12 INFO::-------arguments: vDate 2015-01-07 , previousOutputs 20150105,20150106
2015-01-20 18:53:12 INFO::-------arguments: vDate 2015-01-08 , previousOutputs 20150106,20150107
2015-01-20 18:53:12 INFO::-------arguments: vDate 2015-01-09 , previousOutputs 20150107,20150108
2015-01-20 18:53:12 INFO::-------arguments: vDate 2015-01-10 , previousOutputs 20150108,20150109
[1] "20150110"
> compute("2015-01-08", "2015-01-01", 2)
2015-01-20 18:54:11 INFO::-------arguments: vDate 2015-01-08 , previousOutputs 20150106,20150107
[1] "20150108"
第一个日志是好的,我们每个日期每次只去一次(不用memoize重复)。然而,在第二个日志中,函数auxiliaryCompute
被调用vDate 2015-01-08 , previousOutputs 20150106,20150107
,因为它已经被执行(出现在第一个日志中),这很奇怪。
其他日期是相同的记忆....只有第一个错误...这是因为它字符串和递归中的其他日期被强制为日期格式。
通过在参数中加入日期:
> compute(as.Date("2015-01-08"), "2015-01-01", 2)
[1] "20150108"
它真的很偷偷摸摸,因为R不是一个强类型的语言,而且主要是因为我编码非常糟糕"混乱" 日期和字符串!