Warum ist die Leistung von cgo so langsam? Stimmt etwas mit meinem Testcode nicht?

Ich mache einen Test: Vergleiche die Ausführungszeiten von cgo- und reinen Go-Funktionen, die jeweils 100 Millionen Mal ausgeführt werden. Die cgo-Funktion dauert länger als die Golang-Funktion, und ich bin mit diesem Ergebnis verwirrt. Mein Testcode lautet:

package main

import (
    "fmt"
    "time"
)

/*
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void show() {

}

*/
// #cgo LDFLAGS: -lstdc++
import "C"

//import "fmt"

func show() {

}

func main() {
    now := time.Now()
    for i := 0; i < 100000000; i = i + 1 {
        C.show()
    }
    end_time := time.Now()

    var dur_time time.Duration = end_time.Sub(now)
    var elapsed_min float64 = dur_time.Minutes()
    var elapsed_sec float64 = dur_time.Seconds()
    var elapsed_nano int64 = dur_time.Nanoseconds()
    fmt.Printf("cgo show function elasped %f minutes or \nelapsed %f seconds or \nelapsed %d nanoseconds\n",
        elapsed_min, elapsed_sec, elapsed_nano)

    now = time.Now()
    for i := 0; i < 100000000; i = i + 1 {
        show()
    }
    end_time = time.Now()

    dur_time = end_time.Sub(now)
    elapsed_min = dur_time.Minutes()
    elapsed_sec = dur_time.Seconds()
    elapsed_nano = dur_time.Nanoseconds()
    fmt.Printf("go show function elasped %f minutes or \nelapsed %f seconds or \nelapsed %d nanoseconds\n",
        elapsed_min, elapsed_sec, elapsed_nano)

    var input string
    fmt.Scanln(&input)
}

und das Ergebnis ist:

cgo show function elasped 0.368096 minutes or 
elapsed 22.085756 seconds or 
elapsed 22085755775 nanoseconds

go show function elasped 0.000654 minutes or 
elapsed 0.039257 seconds or 
elapsed 39257120 nanoseconds

Die Ergebnisse zeigen, dass das Aufrufen der C-Funktion langsamer ist als die Go-Funktion. Stimmt etwas mit meinem Testcode nicht?

Mein System ist: Mac OS X 10.9.4 (13E28)

Antworten auf die Frage(6)

Ihre Antwort auf die Frage