Stack Overflow上对Golang性能偏慢的看法

Stack Overflow上对Golang性能偏慢的看法 在我深入学习Go语言的过程中,我系统地查看了Stack Overflow上许多带有Go标签的问题。我收获颇丰,同时也注意到不少过时的问题和回答。特别是有一个问题将Go描述为比Java慢得多。

我觉得应该为Go正名,去点赞一些更近期的基准测试结果:

tiffon

为什么Go(与Java相比)这么慢?

标签: java, performance, go, benchmarking


更多关于Stack Overflow上对Golang性能偏慢的看法的实战教程也可以访问 https://www.itying.com/category-94-b0.html

5 回复

这些基准测试大多测量的是递归函数。Go语言——遗憾的是——没有尾调用优化,因此运行速度较慢。你可以查看这篇文章了解详细说明。

更多关于Stack Overflow上对Golang性能偏慢的看法的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


这是一项更新的测试。Go 和 Java 在运行速度上似乎不相上下,但 Java 使用的内存要多得多。

https://benchmarksgame-team.pages.debian.net/benchmarksgame/faster/go.html

嗨 Taavi,

我对此不会太担心。那是一个非常久远的帖子。我一直在学习 Go 在 Web 开发中的应用,而普遍的共识是 Go 非常快,因为人们通常将其与 Ruby、Python 和 JavaScript/Node.js 进行比较。

至于基准测试,有句老话说得好:

谎言、该死的谎言,还有基准测试。

(这是关于统计的一句更古老说法的变体。)

因为基准测试(就像统计数据一样)永远不会讲述完整的故事,并且很容易被用来歪曲或粉饰真相。

我理解你希望将HTML内容转换为Markdown格式并翻译为中文。根据你提供的HTML内容,主要包含用户信息和帖子内容,但按照你的要求需要移除所有用户名和日期信息。

经过分析,HTML中的主要内容是:

<div class="post" itemprop="text">
  <p><a class="mention" href="/u/jayts">@jayts</a>. I appreciate your push towards not taking benchmarks as ground truth. I would add that just as the saying about statistics doesn't mean statistics is useless, but that careless/manipulative usage of it can lead us away from the truth rather than towards it. So is with benchmarks. Beating the benchmark is not the goal. Improving the performance of something we are trying to measure with benchmarks is. In the current case with Go at Stack Overflow, the statement that Go is significantly slower than Java is not true (any more) according to the benchmarks, and there is no current reason to believe that the results are only a result of careless/manipulative benchmarking.</p>
  <p>As Stack Overflow is highly influencial in the developer community, I felt it was unfair to leave that statement uncorrected with your help.</p>
  <p><a class="mention" href="/u/johandalabacka">@johandalabacka</a> - Thanks. I fixed a broken link in that Stack Overflow post to point to the link you provided.</p>
</div>

由于你要求移除所有用户名和日期信息,最终输出的Markdown文档将只包含翻译后的文本内容,不包含任何用户提及、点赞信息或时间戳。

以下是转换后的Markdown内容:

我理解你推动不要将基准测试视为绝对真理的做法。我想补充的是,就像关于统计学的说法并不意味着统计学无用一样,粗心/操纵性地使用基准测试可能会让我们远离真相而不是接近它。基准测试也是如此。击败基准测试不是目标,通过基准测试改善我们试图测量的性能才是目标。在当前Stack Overflow关于Go的案例中,根据基准测试,Go明显比Java慢的说法不再成立,目前没有理由相信这些结果仅仅是粗心/操纵性基准测试的结果。

由于Stack Overflow在开发者社区中具有很大影响力,我觉得在你的帮助下不纠正这一说法是不公平的。

谢谢。我已经修复了那篇Stack Overflow帖子中的一个失效链接,指向你提供的链接。

Go语言在性能方面实际上表现相当出色,尤其是在并发处理和系统级编程中。Stack Overflow上那个将Go描述为比Java“慢得多”的问题确实过时了,现代Go版本(如Go 1.16+)通过优化编译器、垃圾回收和运行时,性能已大幅提升。以下是一些关键点,结合代码示例说明Go的性能优势:

  1. 并发模型高效:Go的goroutine比Java线程更轻量,上下文切换成本低。例如,处理大量并发任务时,Go可以轻松创建数万个goroutine,而Java线程通常受限于系统资源。

    package main
    import (
        "fmt"
        "time"
    )
    func worker(id int, ch chan int) {
        for task := range ch {
            fmt.Printf("Worker %d processing task %d\n", id, task)
            time.Sleep(10 * time.Millisecond) // 模拟工作
        }
    }
    func main() {
        ch := make(chan int, 100)
        // 启动多个goroutine
        for i := 0; i < 10; i++ {
            go worker(i, ch)
        }
        // 发送任务
        for i := 0; i < 1000; i++ {
            ch <- i
        }
        close(ch)
        time.Sleep(2 * time.Second) // 等待任务完成
    }
    
  2. 编译优化:Go编译为本地代码,无需虚拟机,启动速度快。对比Java的JVM预热,Go应用在冷启动时表现更好。

    package main
    import "fmt"
    func main() {
        // 简单计算示例,展示快速执行
        result := 0
        for i := 0; i < 1000000; i++ {
            result += i
        }
        fmt.Printf("Result: %d\n", result)
    }
    
  3. 垃圾回收改进:Go的垃圾回收器(GC)经过多次优化,暂停时间短,适合低延迟应用。从Go 1.12起,GC已显著减少STW(Stop-The-World)时间。

    package main
    import (
        "runtime"
        "time"
    )
    func allocateMemory() {
        // 模拟内存分配,展示GC效率
        var data [][]byte
        for i := 0; i < 1000; i++ {
            data = append(data, make([]byte, 1024*1024)) // 分配1MB
            time.Sleep(10 * time.Millisecond)
        }
    }
    func main() {
        go allocateMemory()
        time.Sleep(5 * time.Second)
        runtime.GC() // 手动触发GC,实际中自动处理
    }
    
  4. 基准测试数据:根据近期的基准测试(如TechEmpower基准),Go在Web服务、JSON序列化等场景中常与Java持平或更快。例如,使用Go的net/http库处理HTTP请求:

    package main
    import (
        "encoding/json"
        "net/http"
    )
    type Response struct {
        Message string `json:"message"`
    }
    func handler(w http.ResponseWriter, r *http.Request) {
        resp := Response{Message: "Hello, World!"}
        w.Header().Set("Content-Type", "application/json")
        json.NewEncoder(w).Encode(resp)
    }
    func main() {
        http.HandleFunc("/", handler)
        http.ListenAndServe(":8080", nil)
    }
    

总之,Go的性能在多数场景下与Java相当,甚至在并发密集型应用中更优。Stack Overflow上的旧观点不适用于现代Go版本,建议参考官方文档和最新基准测试(如Go博客或GitHub上的性能报告)。

回到顶部