赞
踩
参考《golang java 对比_golang编程语言和java的性能对比》 我们进一步测试Golang和Java的在并发情况下的性能对比
https://blog.csdn.net/weixin_35712223/article/details/114063308
MacBook Pro, Apple M1 MAX, 10核心(8性能2效能), 内存 32G
Golang, go version
go version go1.18.3 darwin/amd64
Java, java -version
- java version "11.0.15" 2022-04-19 LTS
- Java(TM) SE Runtime Environment 18.9 (build 11.0.15+8-LTS-149)
- Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.15+8-LTS-149, mixed mode)
- package main
-
- import (
- "fmt"
- "sync"
- "time"
- )
-
- func main() {
- var wg sync.WaitGroup
- start := time.Now().UnixNano()
- maxCount := 100 //调整并行数量, 调整测试规模
- for i := 0; i < maxCount; i++ {
- count := i
- wg.Add(1)
- go func() {
- defer wg.Done()
- value := fibonacci(50) //fibonacci 递归算法; fibonacciV2 非递归算法
- if count > maxCount-3 {
- fmt.Println(value) //减少不必要的输出, 减少因屏幕输出对测试结果的影响
- }
- }()
- }
- wg.Wait()
- end := time.Now().UnixNano()
- fmt.Printf("Time Consume: %v", (end-start)/1e6)
- }
-
- func fibonacci(i int64) int64 {
- if i < 2 {
- return i
- }
- return fibonacci(i-2) + fibonacci(i-1)
- }
-
- func fibonacciV2(i int64) int64 {
- if i < 2 {
- return i
- }
- var first, second, result, j int64
- first = 0
- second = 1
- result = 0
- for j = 2; j <= i; j++ {
- result = first + second
- first = second
- second = result
- }
- return result
- }
- import java.util.ArrayList;
- import java.util.List;
- import java.util.concurrent.CountDownLatch;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
-
- public class Hello {
-
- public static void main(String[] args){
- long start = System.currentTimeMillis();
- ExecutorService threadPool = Executors.newFixedThreadPool(64);
- int maxCount = 100;
- final CountDownLatch countDownLatch = new CountDownLatch(maxCount);
- List<Thread> workerList = new ArrayList<>();
- for(int i=0;i<maxCount;i++) {
- int count = i;
- threadPool.execute(new Runnable() {
- @Override
- public void run() {
- long value = fibonacci(50);
- if (count>maxCount - 3){
- System.out.println(value);
- }
- countDownLatch.countDown();
- }
- });
- }
- try {
- countDownLatch.await();
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- threadPool.shutdown();
- long end = System.currentTimeMillis();
- System.out.println("Time Consume: "+(end-start));
-
- }
-
- static long fibonacci( long i){
- if(i<2) return i;
- return fibonacci(i-2) + fibonacci(i-1);
- }
-
- static long fibonacciV2( long i){
- if(i<2) return i;
- long first = 0;
- long second = 1;
- long result = 0;
- for(int j=2;j<=i;j++){
- result = first + second;
- first = second;
- second = result;
- }
- return result;
- }
- }
测试场景 | Golang耗时(ms) | Java耗时(ms) | 备注 |
100线程并发 maxCount=100 fibonacci 递归算法 | 746402 | 472179 | 在有N层调用关系的大型系统里, 还是Java表现的更优 |
100线程并发 maxCount=100 fibonacciV2 非递归算法 | 0.662 | 38 | 在调用层次关系更简单的系统里, Golang 表现的更加优秀 |
算法才是系统优化的王道, 简化是系统优化的精髓。
并行测试期间, 都已经把Apple M1 MAX, 10核心(8性能2效能) 的性能使用到了极限。
测试场景 | Golang耗时(ms) | Java耗时(ms) | 备注 |
单线程 maxCount=1 fibonacci 递归算法 | 58484 | 40579 | 在有N层调用关系的大型系统里, 还是Java表现的更优 |
单线程 maxCount=1 fibonacciV2 非递归算法 | 0.138 | 6 | 在调用层次关系更简单的系统里, Golang 表现的更加优秀 |
一定要在相同条件下, 只有一两个条件有差异的情况下, 进行性能对比测试; 因为这样才有可比性。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。