From f4d6eb14b03f5bd10f91414bc0c545b382dc612a Mon Sep 17 00:00:00 2001 From: "Y.D.X" <73375426+YDX-2147483647@users.noreply.github.com> Date: Thu, 27 Jul 2023 21:18:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=BD=E8=B1=A1=E7=9C=9F=E5=8F=AF=E8=83=BD?= =?UTF-8?q?=E5=BC=95=E5=85=A5=E8=B4=9F=E6=88=90=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/2023-07-27-abstraction.md | 42 ++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 docs/2023-07-27-abstraction.md diff --git a/docs/2023-07-27-abstraction.md b/docs/2023-07-27-abstraction.md new file mode 100644 index 0000000..f3720bb --- /dev/null +++ b/docs/2023-07-27-abstraction.md @@ -0,0 +1,42 @@ +# 抽象真可能引入负成本 + +> :fontawesome-regular-face-grin: 洋葱 +> +> :material-clock-edit-outline: 2023年7月27日 21:23:44 + +抽象一般增加无用功能,降低性能。然而另一方面,更清晰的结构也给了编译器更多自由去优化。 + +下面两个 rust 函数功能完全相同,只是第二个`opt1_idiomatic`采用了更抽象的函数式写法。实测`opt1_idiomatic`的性能是`baseline`是几到十几倍。 + +```rust +fn baseline(input: &str) -> i64 { + let mut res = 0; + for b in input.bytes() { + match b { + b's' => res += 1, + b'p' => res -= 1, + _ => continue, + } + } + res +} +``` + +```rust +fn opt1_idiomatic(input: &str) -> i64 { + input + .bytes() + .map(|b| match b { + b's' => 1, + b'p' => -1, + _ => 0, + }) + .sum() +} +``` + +:material-eye-arrow-right-outline: [{n} times faster than C, where n = 128 - Thomas Ip](https://ipthomas.com/blog/2023/07/n-times-faster-than-c-where-n-128/) + +:material-eye-arrow-right-outline: [tommyip/n\_times\_faster\_than\_c: Code for blog post "{n} times faster than C, where n = 128"](https://github.com/tommyip/n_times_faster_than_c) + +作者使用 Apple M1 Pro。如果你不是 ARM 架构,测试前需要删除平台相关的函数。