I have tested several simple functions with Golang and Java. To my surprise, Java sometimes is faster than Golang(especially in recursive function and some function in standard library such as math/rand.Rand). I wonder why. Here is some code I used for test and the result.
Golang code:
package main
import (
    "fmt"
    "math/rand"
    "time"
)
func calPi(pointCount int) float64 {
    inCircleCount := 0
    var x, y float64
    var Pi float64
    for i := 0; i < pointCount; i++ {
        x = rand.Float64()
        y = rand.Float64()
        if x*x+y*y < 1 {
            inCircleCount++
        }
    }
    Pi = (4.0 * float64(inCircleCount)) / float64(pointCount)
    return Pi
}
func fibonacci(c int64) int64 {
    if c < 2 {
        return c
    }
    return fibonacci(c-2) + fibonacci(c-1)
}
func main() {
    rand.Seed(time.Now().Unix()) 
    fmt.Printf("Test 1\n")
    startTime := time.Now()
    result := 0.0
    for i := 0.0; i < 1000000000; i = i + 1 {
        result += i * i
    }
    endTime := time.Now()
    fmt.Printf("Result: %v\n", result)
    fmt.Printf("Duration: %v\n", endTime.Sub(startTime))
    fmt.Printf("Test 2\n")
    startTime = time.Now()
    resultInt := fibonacci(50)
    endTime = time.Now()
    fmt.Printf("Result: %v\n", resultInt)
    fmt.Printf("Duration: %v\n", endTime.Sub(startTime))
    fmt.Printf("Test 3\n")
    startTime = time.Now()
    result = 0.0
    for i := 0.0; i < 100000000; i = i + 1 {
        result += rand.Float64()
    }
    endTime = time.Now()
    fmt.Printf("Result: %v\n", result)
    fmt.Printf("Duration: %v\n s", endTime.Sub(startTime))
    fmt.Printf("Test 4\n")
    startTime = time.Now()
    result = calPi(100000000)
    endTime = time.Now()
    fmt.Printf("Result: %v\n", result)
    fmt.Printf("Duration: %v s\n", endTime.Sub(startTime))
}
the result:
Test 1
Result: 3.333333328333552e+26
Duration: 1.449212507s
Test 2
Result: 12586269025
Duration: 1m31.645050682s
Test 3
Result: 4.999483069673434e+07
Duration: 2.534121566s
 sTest 4
Result: 3.14147056
Duration: 5.036491495s s
Java code:
public class Performance {
    public static double calPi(int pointCount) {
        int inCircleCount = 0;
        double x, y;
        double Pi;
        for (int i = 0; i < pointCount; i++) {
            x = Math.random();
            y = Math.random();
            if (x * x + y * y < 1) {
                inCircleCount++;
            }
        }
        Pi = (4.0 * inCircleCount) / pointCount;
        return Pi;
    }
    public static double cal(double a, double b, double c) {
        return a * b / (c + 1) + a;
    }
    public static long fibonacci(long c) {
        if (c < 2)
            return c;
        return fibonacci(c - 2) + fibonacci(c - 1);
    }
    public static void main(String[] args) {
        System.out.println("Test 1");
        long startTime = System.currentTimeMillis();
        double result = 0.0;
        for (double i = 0.0; i < 1000000000; i = i + 1) {
            result += i * i;
        }
        long endTime = System.currentTimeMillis();
        float duration = (float) (endTime - startTime) / 1000;
        System.out.println("Result: " + result);
        System.out.println("Duration: " + duration + " s");
        System.out.println("Test 2");
        startTime = System.currentTimeMillis();
        long resultInt = fibonacci(50);
        endTime = System.currentTimeMillis();
        duration = (float) (endTime - startTime) / 1000;
        System.out.println("Result: " + resultInt);
        System.out.println("Duration: " + duration + " s");
        System.out.println("Test 3");
        startTime = System.currentTimeMillis();
        result = 0.0;
        for (double i = 0; i < 100000000; i = i + 1) {
            result += Math.random();
        }
        endTime = System.currentTimeMillis();
        duration = (float) (endTime - startTime) / 1000;
        System.out.println("Result: " + result);
        System.out.println("Duration: " + duration + " s");
        System.out.println("Test 4");
        startTime = System.currentTimeMillis();
        result = calPi(100000000);
        endTime = System.currentTimeMillis();
        duration = (float) (endTime - startTime) / 1000;
        System.out.println("Result: " + result);
        System.out.println("Duration: " + duration + " s");
    }
}
result:
Test 1
Result: 3.333333328333552E26
Duration: 2.948 s
Test 2
Result: 12586269025
Duration: 60.816 s
Test 3
Result: 4.9999087237930864E7
Duration: 2.448 s
Test 4
Result: 3.14147284
Duration: 4.786 s
The difference of Test 2 results really shocked me! Please help me to find the reason, thanks. And better if someone could give me the example(s) to show the advantage of Golang (vs Java).
 
    