C++ vs Rust vs Go 性能比较

开发
本文对C++、Rust和Go三种编程语言编写的gunzip程序进行了性能比较,通过基准测试试图尽可能公平的比较它们的性能。

本文将通过一些基准测试,比较 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
责任编辑:赵宁宁 来源: DeepNoMind
相关推荐

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

2020-07-27 08:24:42

编程语言C语言Java

2011-08-02 16:27:50

SQLNoSQL

2024-06-27 07:56:49

2011-04-15 10:26:38

JavaMVC

2009-05-25 08:39:08

iPhone苹果移动OS

2010-01-21 10:04:14

C++语言

2020-05-18 07:00:00

性能测试压力测试负载测试

2020-08-25 09:14:17

对象存储文件存储块存储

2022-04-07 13:56:17

Azure谷歌云AWS

2011-06-08 16:59:04

性能测试载测试压力测试

2023-12-11 08:39:14

Go语言字符串拼

2011-05-18 14:52:04

XML

2013-12-16 10:20:48

MySQL数据库

2023-11-20 10:34:09

语言

2020-07-07 07:00:00

RustGo语言编程语言
点赞
收藏

51CTO技术栈公众号