本文为在WSL下,使用Ubuntu发行版,使用Go 1.22、gccgo 14、llgo 0.10.0分别对相同的代码进行不同的测试。

测试时,gccgo始终带有-O3的flag。llgo由于我没有找到文档,所以并没有添加什么优化flag,故该实验中llgo的情况不具有参考性。

Hello World 链接到标题

代码 链接到标题

package main

func main() {
	println("Hello, World!")
}

构建速度 链接到标题

$ ./build.sh
Building with standard Go compiler...
Successfully built optimized binary with Go (took 0 seconds)
Successfully built regular binary with Go (took 0 seconds)
Building with GCC Go compiler...
Successfully built optimized binary with GCC Go (took 0 seconds)
Successfully built regular binary with GCC Go (took 0 seconds)
Building with LLGO compiler...
Successfully built optimized binary with LLGO (took 5 seconds)
Successfully built regular binary with LLGO (took 4 seconds)
Build process completed

文件大小 链接到标题

直接build时:

$ ls -lh
total 1.6M
-rwxrwxrwx 1 iruanp iruanp 1.4M Mar 13 10:16 main
-rwxrwxrwx 1 iruanp iruanp  46K Mar 13 10:16 main_gccgo
-rwxrwxrwx 1 iruanp iruanp 143K Mar 13 10:16 main_llgo

使用-s -w编译时:

$ ls -lh
total 1.1M
-rwxrwxrwx 1 iruanp iruanp 869K Mar 13 10:16 main
-rwxrwxrwx 1 iruanp iruanp  23K Mar 13 10:16 main_gccgo
-rwxrwxrwx 1 iruanp iruanp 143K Mar 13 10:16 main_llgo //可能是LLGO没有办法使用这两个flag,但我没有找到相关文档

结论:编译出的文件的体积 go>llgo>gccgo

斐波那契数列 链接到标题

代码 链接到标题

package main

import (
	"fmt"
	"time"
)

func fibonacci(n int) uint64 {
	if n <= 1 {
		return uint64(n)
	}

	a, b := uint64(0), uint64(1)
	for i := 2; i <= n; i++ {
		a, b = b, a+b
	}
	return b
}

func main() {
	// Using a hardcoded number for testing
	n := 93

	start := time.Now()
	result := fibonacci(n)
	duration := time.Since(start)

	fmt.Printf("Fibonacci(%d) = %d\n", n, result)
	fmt.Printf("Time taken: %v\n", duration)
}

构建速度 链接到标题

测试构建速度:

$ ./build.sh
Building with standard Go compiler...
Successfully built optimized binary with Go (took 0 seconds)
Successfully built regular binary with Go (took 0 seconds)
Building with GCC Go compiler...
Successfully built optimized binary with GCC Go (took 1 seconds)
Successfully built regular binary with GCC Go (took 0 seconds)
Building with LLGO compiler...
Successfully built optimized binary with LLGO (took 8 seconds)
Successfully built regular binary with LLGO (took 7 seconds)
Build process completed

结论:在构建速度上,go>gccgo>llgo

文件大小 链接到标题

build:

$ ls -lh
total 3.4M
-rwxrwxrwx 1 iruanp iruanp 1.9M Mar 13 10:20 main
-rwxrwxrwx 1 iruanp iruanp  64K Mar 13 10:20 main_gccgo
-rwxrwxrwx 1 iruanp iruanp 1.5M Mar 13 10:20 main_llgo

-s -w

$ ls -lh
total 2.8M
-rwxrwxrwx 1 iruanp iruanp 1.2M Mar 13 10:20 main
-rwxrwxrwx 1 iruanp iruanp  37K Mar 13 10:20 main_gccgo
-rwxrwxrwx 1 iruanp iruanp 1.5M Mar 13 10:20 main_llgo

运行速度 链接到标题

测试运行速度:

$ ./run.sh
=== Running binaries from build directory ===
\nRunning: build/main
Fibonacci(93) = 12200160415121876738
Time taken: 118ns
Time taken: 0 seconds
\nRunning: build/main_gccgo
Fibonacci(93) = 12200160415121876738
Time taken: 169ns
Time taken: 0 seconds
\nRunning: build/main_llgo
Fibonacci(93) = 12200160415121876738
Time taken: 338ns
Time taken: 0 seconds
\n=== Running binaries from build-flags directory ===
\nRunning: build-flags/main
Fibonacci(93) = 12200160415121876738
Time taken: 119ns
Time taken: 0 seconds
\nRunning: build-flags/main_gccgo
Fibonacci(93) = 12200160415121876738
Time taken: 294ns
Time taken: 1 seconds
\nRunning: build-flags/main_llgo
Fibonacci(93) = 12200160415121876738
Time taken: 229ns
Time taken: 0 seconds

再进行一次测试:

$ ./run.sh
=== Running binaries from build directory ===
\nRunning: build/main
Fibonacci(93) = 12200160415121876738
Time taken: 114ns
Time taken: 0 seconds
\nRunning: build/main_gccgo
Fibonacci(93) = 12200160415121876738
Time taken: 169ns
Time taken: 0 seconds
\nRunning: build/main_llgo
Fibonacci(93) = 12200160415121876738
Time taken: 244ns
Time taken: 0 seconds
\n=== Running binaries from build-flags directory ===
\nRunning: build-flags/main
Fibonacci(93) = 12200160415121876738
Time taken: 130ns
Time taken: 0 seconds
\nRunning: build-flags/main_gccgo
Fibonacci(93) = 12200160415121876738
Time taken: 172ns
Time taken: 0 seconds
\nRunning: build-flags/main_llgo
Fibonacci(93) = 12200160415121876738
Time taken: 262ns
Time taken: 0 seconds

不知道llgo是否有进一步优化用的flag。gccgo如果不带O3参数,用时和llgo的差不多。

结论:性能都差不多

总结 链接到标题

go build cmd/main.go is all you need. (IMHO)

至少对我来说没有必要浪费时间去额外配置gccgo和llgo,只需要直接使用go构建项目。