golang与java并发性能对比测试

测试环境:cpu:2.8 GHz 四核Intel Core i7
内存:16 GB 1600 MHz DDR3

jdk版本:1.8
go版本:1.14

测试方法:分别使用golang和java并发执行相同数量的空任务
golang使用goroutine实现,代码如下:

func main() {
    count,line := 100*10000,"-------------------------------------"
    runTask(count)
    fmt.Println(line)
    count = 1000*10000
    runTask(count)
    fmt.Println(line)
    count = 10000*10000
    runTask(count)
}

func runTask(taskCount int)  {
    runtime.GOMAXPROCS(runtime.NumCPU())
    fmt.Println("golang并发测试")
    fmt.Printf("processors=%d\n", runtime.NumCPU())
    fmt.Printf("tasks=%d\n", taskCount)
    t1 := time.Now()
    for i:=0; i 4 {
    //fmt.Println("current goroutines:", runtime.NumGoroutine())
    //time.Sleep(time.Second)
    //}
    t2 := time.Now()
    fmt.Printf("cost time: %.3fs\n", t2.Sub(t1).Seconds())
}

java使用线程池实现,代码如下:

public static void main(String[] args) throws Exception {
            int count = 100*10000;
            String line = "-------------------------------------";
            runTask(count);
            System.out.println(line);
            count = 1000*10000;
            runTask(count);
            System.out.println(line);
            count = 10000*10000;
            runTask(count);
        }

        public static void runTask(int taskCount){
            int d = Runtime.getRuntime().availableProcessors();
            System.out.println("java并发测试");
            System.out.printf("processors=%d \n",d);
            System.out.printf("tasks=%d\n",taskCount);
            ExecutorService service = Executors.newFixedThreadPool(d);
            long start = System.currentTimeMillis();
            for (int i=0;i {});
            }
            service.shutdown();
            long end = System.currentTimeMillis();
            System.out.printf("cost time: %.3fs\n", (end-start)/1000f);
        }

golang测试结果:
golang并发测试
processors=8
tasks=1000000
cost time: 0.291s


golang并发测试
processors=8
tasks=10000000
cost time: 3.090s


golang并发测试
processors=8
tasks=100000000
cost time: 34.591s

java测试结果:
java并发测试
processors=8
tasks=1000000
cost time: 0.313s


java并发测试
processors=8
tasks=10000000
cost time: 6.239s


java并发测试
processors=8
tasks=100000000
Exception in thread "pool-3-thread-1"

结论:golang在处理并发上要优于java!
当并发在百万量级时,golang比java快7%,优势不明显;
当并发在千万量级时,golang比java快2倍以上,优势明显;
当并发在1亿时,golang能够在35秒内处理完成,而java则会在数分钟后抛异常导致程序崩溃。

你可能感兴趣的:(golang与java并发性能对比测试)