C++ vs Rust vs Go 性能比較
本文將通過一些基準測試,比較 C++ 和 Rust 以及 Go 編寫的相同程序的性能。我們將盡最大努力將語言差異以外的噪音因素隔離開來,不過,與任何基準測試一樣,需要慎重對待測試結果,因為沒有任何一種基準測試能真正比較兩種不同語言的性能。
計劃
本文要比較的程序是 gunzip,它可以解壓 .gz 文件。gunzip 有不同的實現,例如用 C 編寫的 GNU gzip[2]、用 C 編寫的 zlib[3]、用 C 編寫的 miniz[4]、用 Rust 編寫的 flate2-rs[5] 和用 Go 編寫的 gzip[6]。
但是,除非一種語言是另一種語言的直接移植,由于可能會引入不同實現的噪音,因此無法對兩種語言進行準確的基準測試。
為此,我們將選擇以下三個方面:
- 用 Rust 寫的 gunzip[7]
- C++ 編寫的移植版cpp_gunzip[8]
- Go 編寫的移植版go_gunzip[9]
盡量減少噪音
還有一個問題--外部庫。它們都依賴第三方庫計算 CRC32 校驗和,這在解壓縮過程中會耗費大量時間。其中,gunzip 依賴 crc32fast[10],cpp_gunzip 可以鏈接 zlib 或 FastCrc32[11],而 go_gunzip 則依賴 Go 標準庫里的 crc32[12]。幸運的是,所有這些程序都支持多線程選項,可以在單獨的線程上運行 CRC32 校驗和,因此運行時間與解壓縮實現成正比--這是因為解壓縮比 CRC32 校驗和耗時更長,因此通過并行化,可以有效的將 CRC32 校驗和的影響降至最低。
讓我們做一些實驗來驗證。我們用兩種不同的方式編譯 cpp_gunzip:(1) 使用 FastCrc32;(2) 使用 zlib 計算 CRC32 校驗和。然后使用單線程和雙線程模式比較兩者的運行時間,看看有什么不同。
# terminal in Linux
git clone https://github.com/TechHara/cpp_gunzip.git
cd cpp_gunzip
# compile with FastCrc32 vs zlib for CRC32 checksum
cmake -B fastcrc32 -DCMAKE_CXX_FLAGS=-O3 -DUSE_FAST_CRC32=ON . && make -j -C fastcrc32
cmake -B zlib -DCMAKE_CXX_FLAGS=-O3 -DUSE_FAST_CRC32=OFF . && make -j -C zlib
# download linux source code and compress as .gz file
curl -o- https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.8.7.tar.xz | xz -d | gzip > linux.tgz
# run with single-thread
time fastcrc32/gunzip < linux.tgz > linux.tar
time zlib/gunzip < linux.tgz > linux.tar
# run with multi-thread (-t option)
time fastcrc32/gunzip -t < linux.tgz > linux.tar
time zlib/gunzip -t < linux.tgz > linux.tar
在 x64 Ubuntu 系統上,單線程模式下兩個 CRC32 校驗和庫的性能差別很大。不過,當我們在多線程模式下運行時,這兩個庫的運行時間并沒有出現預期的差異。因此,這讓我們可以最大限度減少基準測試時使用不同 CRC32 庫所帶來的噪音。
基準測試
接下來我們將運行基準,使用完全相同的 .gz 解壓縮實現,比較 C++ 與 Rust 和 Go 的性能。我們已經運行了 C++ 版本,現在來運行 Rust 和 Go 版本。確保在多線程模式下運行,以盡量減少 CRC32 校驗和產生的噪音。
# clone the Rust version
git clone https://github.com/TechHara/gunzip.git
cd gunzip
# build
cargo build -r
# run in multi-threaded mode (-t)
time target/release/gunzip -t < ../linux.tgz > linux.tar
# clone the Go version
cd ..
git clone https://github.com/TechHara/go_gunzip.git
cd go_gunzip
# build
go build
# set max process to 2
export GOMAXPROCS=2
# run in multi-threaded mode (-t)
time ./gunzip -t < ../linux.tgz > linux.tar
好吧,在 x64 Ubuntu 系統上,C++ 和 Rust 的運行速度幾乎相同,而 Go 的運行時間是它們的 2 倍左右。但與benchmarkgame的數據(4倍)相比,在這個場景下的Go性能還更好一點。
https://benchmarksgame-team.pages.debian.net/benchmarksgame/index.html
但更好的性能并不意味著更好的語言。在選擇語言時,必須考慮應用、開發/維護時間以及安全性。最典型的例子就是 Python,它比 C 語言慢 100 倍,但卻是最流行的編程語言。
參考資料:
- [1]Performance — C++ vs Rust vs Go: https://medium.com/@techhara/performance-c-vs-rust-vs-go-a44cbd2cc882
- [2]GUN gzip: https://www.gnu.org/software/gzip
- [3]zlib: https://www.zlib.net
- [4]miniz: https://github.com/richgel999/miniz
- [5]flate2-rx: https://github.com/rust-lang/flate2-rs
- [6]gzip in Go: https://pkg.go.dev/compress/gzip
- [7]gunzip in Rust: https://github.com/techhara/gunzip
- [8]cpp_gunzip: https://github.com/TechHara/cpp_gunzip
- [9]go_gunzip: https://github.com/TechHara/go_gunzip
- [10]crc32fast: https://docs.rs/crc32fast/latest/crc32fast/
- [11]FastCrc32: https://create.stephan-brumme.com/crc32
- [12]crc32: https://pkg.go.dev/hash/crc32