這篇文章主要介紹了golang、java性能的對比,須要的朋友能夠參考下!java

1.先說Go吧,由於我我的最近正在用,感受很不錯c++

package main
import "fmt"
func main(){
  fmt.Println(fibonacci(34))
}
func fibonacci(i int) int{
  if(i<2){
    return i;
  }
  return fibonacci(i-2) + fibonacci(i-1);
}
複製代碼

先用 Go1.7看看:golang

複製代碼bash

代碼以下:性能

qiangjian@localhost:/works/learnCPP$ go version && time go build fib.go && time ./fib優化

go version go1.7.5 darwin/amd64real 0m0.206sui

user 0m0.165sspa

sys 0m0.059scode

real 0m0.052sci

user 0m0.045s

sys 0m0.004s

而後,再看看1.8的:

代碼以下:

qiangjian@localhost:/works/learnCPP$ go18 version && time go18 build fib.go && time ./fib

go version go1.8 darwin/amd64real 0m0.204s

user 0m0.153s

sys 0m0.062s

real 0m0.051s

user 0m0.045s

sys 0m0.003s

感受看不出差別,但官方1.8在GC、Compile等方面優化提高了20%,多是這demo太簡單了吧。

2.再來寫Java,雖然它很火,感受太臃腫了

class Fib{
  public  static void main(String[] args){
    System.out.println(fibonacci(34));
 
  }
 
  static int fibonacci( int i){
    if(i<2) return i;
    return fibonacci(i-2) + fibonacci(i-1);
  }
}
複製代碼

編譯、運行的結果是:

qiangjian@localhost:/works/learnCPP$ java -version && time javac Fib.java && time java Fib 
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)
real  0m0.952s
user  0m1.302s
sys   0m0.144s
real  0m0.150s
user  0m0.123s
sys   0m0.025s
複製代碼

性能還行, Compile時間和c++/go比就太low了。