簡體   English   中英

嘗試從python腳本執行golang程序時出現錯誤

[英]Getting an error on trying to execute a golang program from a python script

我正在對C ++和GoLang之間的性能比較進行編程,以獲取數據來進行統計分析,並且我創建了一個Python腳本來獲取所有數據並由兩個程序自己執行。 使用C ++,我沒有問題,執行正常,但是在執行過程中出現以下錯誤:

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源代碼。

蟒蛇:

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

走:

package main

import "strconv"
import "os"

func merge(v []int, e int, m int, d int) {
    size := d-e+1
    B := make([]int, size)
    i := e
    j := m + 1
    k := 0
    for i <= m && j <= d {
        if v[i] <= v[j] {
            B[k] = v[i]
            k++
            i++
        } else {
            B[k] = v[j]
            k++
            j++
        }
    }
    for i <= m {
        B[k] = v[i]
        k++
        i++
    }
    for j <= d {
        B[k] = v[j]
        k++
        j++
    }
    for k := 0; k <= d-e; k++ {
        v[e+k] = B[k]
    }
}

func mergesort(v []int, e int, d int) {
    if e < d {
        m := (e+d)/2
        mergesort(v, e, m)
        mergesort(v, m+1, d)
        merge(v, e, m, d)
    }
}

func main() {
    v := make([]int, 0)
    args := os.Args
    for i := 1; i < len(args); i++ {
        f,_ := strconv.Atoi(os.Args[i])
        v = append(v, f)
    }
    mergesort(v, 0, len(args) - 1)
}

問題不在您的Python中,因為Go程序會得到相同的錯誤,而不會被Python調用。 main函數中,您將從參數位置1開始創建一個名為v的切片,因此不包括程序名稱:

for i := 1; i < len(args); i++ {
    f,_ := strconv.Atoi(os.Args[i])
    v = append(v, f)
}

但是,然后您給mergesort賦予了args的長度,而不是v的長度,所以這是一個錯誤的錯誤,因為它被告知v比實際多了1個元素。 程序開始嘗試訪問v的元素,該元素超出切片的末尾:

mergesort(v, 0, len(args) - 1)

嘗試在主函數中使用mergesort(v, 0, len(v) - 1)

嘗試更改:

call(command)

call(command, shell=True)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM