Groovy列表/映射的性能问题

时间:2016-03-31 12:11:00

标签: groovy

我有这个代码比较两个列表并找到差异,到目前为止这么好,它适用于小列表。现在我正在测试巨大的列表。 其中包含两个多于 300000 的地图。处理它需要超过 5小时。那是正常的吗?如何减少占用时间?

def list1 = [
    [cuInfo:"T12",service:"3",startDate:"14-01-16 13:22",appId:"G12355"],
    [cuInfo:"T13",service:"3",startDate:"12-02-16 13:00",appId:"G12356"],
    [cuInfo:"T14",service:"9",startDate:"10-01-16 11:20",appId:"G12300"], 
    [cuInfo:"T15",service:"10",startDate:"26-02-16 10:20",appId:"G12999"]
]
​
def list2 = [
    [name:"testname1",cuInfo:"T12",service:"3",startDate:"14-02-16 10:00",appId:"G12351"],
    [name:"testname1",cuInfo:"T13",service:"3",startDate:"14-01-16 13:00",appId:"G12352"],
    [name:"testname1",cuInfo:"T16",service:"3",startDate:"14-01-16 13:00",appId:"G12353"],
    [name:"testname2",cuInfo:"T14",service:"9",startDate:"10-01-16 11:20",appId:"G12301"], 
    [name:"testname3",cuInfo:"T15",service:"10",startDate:"26-02-16 10:20",appId:"G12999"],
    [name:"testname3",cuInfo:"T18",service:"10",startDate:"26-02-16 10:20",appId:"G12999"]  
]

def m1 = [:]
def m2 = [:]
def rows = list1.collect { me -> 
    [me, list2.find { it.cuInfo == me.cuInfo && it.service == me.service }] 
}.findAll {
            it[1]
}.findAll { 
    /*
     * This is where the differences are identified.
     * The 'name' attribute is excluded from the comparison,
     * by including only the desired attributes.
     */
    it[0] != it[1].subMap(['cuInfo', 'service', 'startDate', 'appId'])
}.collect {
    /*
     * At this point the list only contains the row pairs
     * which are different. This step identifies which columns
     * are different using asterisks.
     */
    (m1, m2) = it
    m1.keySet().each { key ->
        if(m1[key] != m2[key]) {
            m1[key] = "*${m1[key]}*"
            m2[key] = "*${m2[key]}*"
        }          
}

                [m1, m2]
            }.collect {
    [it[0].values(), it[1].values()].flatten() as String[]
}

1 个答案:

答案 0 :(得分:0)

也许这会有所帮助。我没有时间进行测试,但您的代码有很多收集并找到可能导致性能问题的所有内容

netstat -an | grep 8000