我正在编写 C++ 和 GoLang 之间的性能比较以获取数据以执行统计分析,并且我创建了一个 Python 脚本来获取所有数据并自行执行这两个程序。使用 C++ 我没有问题并且执行工作正常,但是在 go 中我得到了这个错误:


panic: runtime error: index out of range


goroutine 1 [running]:

runtime.panic(0x44d600, 0x4b9897)

    /usr/lib/go/src/pkg/runtime/panic.c:266 +0xb6

main.merge(0xc210047000, 0x9, 0x10, 0x8, 0x8, ...)

    /windows/DATA/FIB/PE/B7_PLSpeed/nlogn.go:13 +0x22c

main.mergesort(0xc210047000, 0x9, 0x10, 0x8, 0x9)

    /windows/DATA/FIB/PE/B7_PLSpeed/nlogn.go:43 +0xec

main.mergesort(0xc210047000, 0x9, 0x10, 0x5, 0x9)

    /windows/DATA/FIB/PE/B7_PLSpeed/nlogn.go:42 +0xac

main.mergesort(0xc210047000, 0x9, 0x10, 0x0, 0x9)

    /windows/DATA/FIB/PE/B7_PLSpeed/nlogn.go:42 +0xac

main.main()

    /windows/DATA/FIB/PE/B7_PLSpeed/nlogn.go:54 +0x1c1

如果有人可以帮助我,我会非常高兴!我将在这里留下我的 Python 脚本和 Go 源代码。


Python:


import time

import random

from subprocess import call

from sys import argv



def usage():

    print("Usage: " + argv[0] + " <Binary1> <Binary2> <n elements> <#executions>")


if __name__ == '__main__':

    if len(argv) != 5:

        usage()

    else:

        program1 = argv[1]

        program2 = argv[2]

        n = int(argv[3])

        executions = int(argv[4])


        for x in range(0, executions):

            command = ['']

            # 32-bit range vector(n) random generator

            for y in range(0, n-1):

                command.append(str(random.randint(-2147473648, 2147473647)))


            if(random.choice((True, False))):

                program1, program2 = program2, program1


            # Program1

            command[0] = './' + program1

            pre = time.time()

            call(command)

            post = time.time()

            f = open(program1 + ' - ' + str(n), 'a')

            f.write(str(post-pre) + "ms\n")

            f.close


            # Program2

            command[0] = './' + program2

            pre = time.time()

            call(command)

            post = time.time()

            f = open(program2 + ' - ' + str(n), 'a')

            f.write(str(post-pre) + "ms\n")

            f.close