与串行相比,Groovy Gpars的并行性能较差

时间:2017-03-21 10:35:37

标签: windows multithreading groovy gpars

在i7-2960xm(4核超线程)上尝试多线程时,我遇到的Groovy Gpars性能比预期差。在我的测试中,我一直在使用递归的fib计算器来模拟工作量:

def fibRecursive(int index) {
    if (index == 0 || index == 1) {
        return index
    }
    else {
        return fibRecursive(index - 2) + fibRecursive(index - 1)
    }
}

要测试Gpars,我目前正在使用以下代码:

def nums = [36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36]

GParsPool.withPool(4) {
    nums.eachParallel {
        SplitTimer internalTimer = new SplitTimer()
        println("fibRecursive(${it}): ${fibRecursive(it)}")
        internalTimer.split("fibRecursive(${it})")

        for (instance in internalTimer.splitTimes) {
            println(instance)
        }
    }
}

fib(36)并行计算withPool(4)大约需要1.9秒。 withPool(1)需要大约1.4秒,我认为这与在Gpars之外调用函数有点相似,但只需要0.4秒,例如:

nums.each {
    SplitTimer internalTimer = new SplitTimer()
    println("fibRecursive(${it}): ${fibRecursive(it)}")
    internalTimer.split("fibRecursive(${it})")

    for (instance in internalTimer.splitTimes) {
        println(instance)
    }
}

有人可以解释为什么我可能会遇到这种性能打击吗?谢谢!

以下是我的SplitTimer:

class SplitTimer {
    long initialTime
    int instances = 0

    class Instance {
        int index
        String name
        long time

        def elapsed() {
            return time - initialTime
        }

        def Instance(String instanceName) {
            this.index = this.instances++
            this.name = instanceName
            this.time = System.nanoTime()
        }

        String toString() {
            return "[Instance ${this.index}: \"${this.name}\" (${Formatter.elapsed(this.elapsed())} elapsed)]"
        }
    }

    def splitTimes = []

    def SplitTimer() {
        def initialInstance = new Instance("Start")
        this.initialTime = initialInstance.time
        splitTimes.add(initialInstance)
    }

    def split(String instanceName) {
        splitTimes.add(new Instance(instanceName))
    }
}

class Formatter {
    static int hours
    static int minutes
    static int seconds
    static int nanoseconds

    static setValues(time) {
        nanoseconds = time % 10**9

        seconds = time / 10**9
        minutes = seconds / 60
        hours = minutes / 60

        seconds %= 60
        minutes %= 60
    }

    static elapsed(time) {
        setValues(time)
        return "${hours}:" + "${minutes}:".padLeft(3, "0") + "${seconds}.".padLeft(3, "0") + "${nanoseconds}".padLeft(9,"0")
    }

    static absolute(time) {
        setValues(time)
        hours %= 24
        return "${hours}:".padLeft(3, "0") + "${minutes}:".padLeft(3, "0") + "${seconds}.".padLeft(3, "0") + "${nanoseconds}".padLeft(9,"0")
    }
}

1 个答案:

答案 0 :(得分:0)

并行化少量函数调用所需的工作量可能超过顺序运行它们所需的工作量。但是,如果您将斐波纳契称为非常大的数字,则可能会发生这种情况。