【发布时间】:2015-04-07 20:37:55
【问题描述】:
我对 Groovy/Grails 领域还很陌生。我最近修改了一些代码以添加以下块。
result.processed.each{
def queueEntry = QueueEntry.findById(it.id)<<<START ADD>>>
Set dates = new HashSet<Long>()
def children = QueueEntry.findAllByParent(queueEntry)
for(QueueEntry qe : children){
def f = new GregorianCalendar()
f.setTimeInMillis(DateUtils.getClearedTime(qe.entryTimestamp))
def l = new GregorianCalendar()
l.setTimeInMillis(DateUtils.getClearedTime(qe.exitTimestamp))
while(f < l){
if(f.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY && f.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY){//only add weekdays
dates.add(f.time.time)
}
def xx = new GregorianCalendar()
xx.setTimeInMillis(f.time.next().time)
f = xx
}
dates.add(l.time.time)
} <<<STOP ADD>>>
Set outsideDays = it.numberOfDaysOutsideCVB
Set days = DateUtils.businessDaysBetweenDates(it.entryTimestamp, it.exitTimestamp)
days.removeAll(outsideDays)
days.removeAll(dates)
turnTimes << days.size()
}
应用程序现在正在爬行。我显然做错了什么。当它针对小型数据集运行时,它将缓慢完成。在较大的集合上,它不会完成。在此更改之前,它已完成。
【问题讨论】:
-
通常,由于休眠映射做了一些意想不到的事情,数据库访问可能会成为问题。打开休眠日志记录(调试“org.hibernate.SQL”),看看它在做什么。
-
看一下while循环里面的代码。它嵌套在 for 循环中,因此您在此块中存在潜在的瓶颈。你在这里尝试过什么吗?也许构造一个 GregorianCalender 对象的成本太高而不能做这么多次?
-
你确定你的while循环退出了吗?此外,您现在有 3 个嵌套循环(每个、for、while)
-
@zcleghern 我确实尝试更改代码以不每次都构造 GregorianCalender 对象,并且速度稍快。感谢您的建议。
标签: groovy