自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C++ vs Rust vs Go 性能比較

開(kāi)發(fā)
本文對(duì)C++、Rust和Go三種編程語(yǔ)言編寫的gunzip程序進(jìn)行了性能比較,通過(guò)基準(zhǔn)測(cè)試試圖盡可能公平的比較它們的性能。

本文將通過(guò)一些基準(zhǔn)測(cè)試,比較 C++ 和 Rust 以及 Go 編寫的相同程序的性能。我們將盡最大努力將語(yǔ)言差異以外的噪音因素隔離開(kāi)來(lái),不過(guò),與任何基準(zhǔn)測(cè)試一樣,需要慎重對(duì)待測(cè)試結(jié)果,因?yàn)闆](méi)有任何一種基準(zhǔn)測(cè)試能真正比較兩種不同語(yǔ)言的性能。

計(jì)劃

本文要比較的程序是 gunzip,它可以解壓 .gz 文件。gunzip 有不同的實(shí)現(xiàn),例如用 C 編寫的 GNU gzip[2]、用 C 編寫的 zlib[3]、用 C 編寫的 miniz[4]、用 Rust 編寫的 flate2-rs[5] 和用 Go 編寫的 gzip[6]。

但是,除非一種語(yǔ)言是另一種語(yǔ)言的直接移植,由于可能會(huì)引入不同實(shí)現(xiàn)的噪音,因此無(wú)法對(duì)兩種語(yǔ)言進(jìn)行準(zhǔn)確的基準(zhǔn)測(cè)試。

為此,我們將選擇以下三個(gè)方面:

  • 用 Rust 寫的 gunzip[7]
  • C++ 編寫的移植版cpp_gunzip[8]
  • Go 編寫的移植版go_gunzip[9]

盡量減少噪音

還有一個(gè)問(wèn)題--外部庫(kù)。它們都依賴第三方庫(kù)計(jì)算 CRC32 校驗(yàn)和,這在解壓縮過(guò)程中會(huì)耗費(fèi)大量時(shí)間。其中,gunzip 依賴 crc32fast[10],cpp_gunzip 可以鏈接 zlib 或 FastCrc32[11],而 go_gunzip 則依賴 Go 標(biāo)準(zhǔn)庫(kù)里的 crc32[12]。幸運(yùn)的是,所有這些程序都支持多線程選項(xiàng),可以在單獨(dú)的線程上運(yùn)行 CRC32 校驗(yàn)和,因此運(yùn)行時(shí)間與解壓縮實(shí)現(xiàn)成正比--這是因?yàn)榻鈮嚎s比 CRC32 校驗(yàn)和耗時(shí)更長(zhǎng),因此通過(guò)并行化,可以有效的將 CRC32 校驗(yàn)和的影響降至最低。

讓我們做一些實(shí)驗(yàn)來(lái)驗(yàn)證。我們用兩種不同的方式編譯 cpp_gunzip:(1) 使用 FastCrc32;(2) 使用 zlib 計(jì)算 CRC32 校驗(yàn)和。然后使用單線程和雙線程模式比較兩者的運(yùn)行時(shí)間,看看有什么不同。

# 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 系統(tǒng)上,單線程模式下兩個(gè) CRC32 校驗(yàn)和庫(kù)的性能差別很大。不過(guò),當(dāng)我們?cè)诙嗑€程模式下運(yùn)行時(shí),這兩個(gè)庫(kù)的運(yùn)行時(shí)間并沒(méi)有出現(xiàn)預(yù)期的差異。因此,這讓我們可以最大限度減少基準(zhǔn)測(cè)試時(shí)使用不同 CRC32 庫(kù)所帶來(lái)的噪音。

基準(zhǔn)測(cè)試

接下來(lái)我們將運(yùn)行基準(zhǔn),使用完全相同的 .gz 解壓縮實(shí)現(xiàn),比較 C++ 與 Rust 和 Go 的性能。我們已經(jīng)運(yùn)行了 C++ 版本,現(xiàn)在來(lái)運(yùn)行 Rust 和 Go 版本。確保在多線程模式下運(yùn)行,以盡量減少 CRC32 校驗(yàn)和產(chǎn)生的噪音。

# 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 系統(tǒng)上,C++ 和 Rust 的運(yùn)行速度幾乎相同,而 Go 的運(yùn)行時(shí)間是它們的 2 倍左右。但與benchmarkgame的數(shù)據(jù)(4倍)相比,在這個(gè)場(chǎng)景下的Go性能還更好一點(diǎn)。

https://benchmarksgame-team.pages.debian.net/benchmarksgame/index.html

但更好的性能并不意味著更好的語(yǔ)言。在選擇語(yǔ)言時(shí),必須考慮應(yīng)用、開(kāi)發(fā)/維護(hù)時(shí)間以及安全性。最典型的例子就是 Python,它比 C 語(yǔ)言慢 100 倍,但卻是最流行的編程語(yǔ)言。

參考資料:

  • [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
責(zé)任編輯:趙寧寧 來(lái)源: DeepNoMind
相關(guān)推薦

2023-11-19 21:17:58

GoRust

2019-05-24 08:48:33

JSONJacksonJSONP

2019-04-02 15:07:51

API NginxZuul

2022-12-15 08:54:28

JAVA性能JDK

2017-11-23 22:32:18

框架ScrumXP

2011-08-02 16:27:50

SQLNoSQL

2020-07-27 08:24:42

編程語(yǔ)言C語(yǔ)言Java

2024-06-27 07:56:49

2011-04-15 10:26:38

JavaMVC

2010-01-21 10:04:14

C++語(yǔ)言

2020-05-18 07:00:00

性能測(cè)試壓力測(cè)試負(fù)載測(cè)試

2011-06-08 16:59:04

性能測(cè)試載測(cè)試壓力測(cè)試

2009-05-25 08:39:08

iPhone蘋果移動(dòng)OS

2020-07-07 07:00:00

RustGo語(yǔ)言編程語(yǔ)言

2020-08-25 09:14:17

對(duì)象存儲(chǔ)文件存儲(chǔ)塊存儲(chǔ)

2022-04-07 13:56:17

Azure谷歌云AWS

2011-05-18 14:52:04

XML

2018-05-07 10:04:55

ServerlessGoogle ClouAzure

2023-12-11 08:39:14

Go語(yǔ)言字符串拼

2013-12-16 10:20:48

MySQL數(shù)據(jù)庫(kù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)