测试代码:
itang@itang-laptop:~/test$ gedit testgo.go
package main
import "fmt"
func main(){
var sum int64 = 0
for i :=int64(0); i < 10000000000; i++ {
sum += i
}
fmt.Printf("sum: %d\n", sum)
}
itang@itang-laptop:~/test$ gedit TestScala.scala
object TestScala{
def main(args: Array[String]){
var sum : Long = 0
var i: Long = 0
while(i < 10000000000L){
sum = sum + i
i = i + 1
}
println("sum: " + sum)
}
}
itang@itang-laptop:~/test$ gedit TestJava.java
class TestJava{
public static void main(String[] args){
long sum =0;
for(long i =0;i<10000000000L;i++){
sum += i;
}
System.out.println("sum: " + sum);
}
}
版本信息:
itang@itang-laptop:~$ 8g -V
8g version 6745
itang@itang-laptop:~$ scala -version
Scala code runner version 2.8.1.final -- Copyright 2002-2010, LAMP/EPFL
itang@itang-laptop:~$ java -version
java version "1.6.0_22"
Java(TM) SE Runtime Environment (build 1.6.0_22-b04)
Java HotSpot(TM) Server VM (build 17.1-b03, mixed mode)
运行测试:
itang@itang-laptop:~/test$ 8g testgo.go && 8l -o testgo testgo.8 && time ./testgo
sum: -5340232226128654848
real 0m23.938s
user 0m23.873s
sys 0m0.000s
itang@itang-laptop:~/test$ scalac TestScala.scala && time scala TestScala
sum: -5340232226128654848
real 0m34.055s
user 0m33.934s
sys 0m0.100s
itang@itang-laptop:~/test$ javac TestJava.java && time java TestJava
sum: -5340232226128654848
real 0m34.709s
user 0m34.554s
sys 0m0.032s
总结:
1 go 在基本类型运算中速度是超越java和scala的
2 scala和java版的测试代码编译之后的字节码应该基本相同(scala支持Long为基本类型)
所以scala和java在本次测试中性能差不多