< 返回版块

vsylva 发表于 2025-09-01 23:02

use criterion::{criterion_group, criterion_main, BatchSize, Criterion};
use std::{borrow::Cow, hint::black_box};

fn sample_data() -> Vec<&'static str> {
    vec![
        "",
        "a",
        "hello",
        "rustacean",
        "The quick brown fox jumps over the lazy dog",
        "The quick brown fox jumps over the lazy dog The quick brown fox jumps over the lazy dog The quick brown fox jumps over the lazy dog",
        "🚀✨🦀",
        "🚀✨🦀🚀✨🦀🚀✨🦀🚀✨🦀🚀✨🦀🚀✨🦀",
    ]
    .into_iter()
    .collect()
}

fn bench_to_owned(c: &mut Criterion) {
    let data = sample_data();

    let mut g = c.benchmark_group("str_clone_methods");

    g.bench_function("to_owned", |b| {
        b.iter_batched(
            || data.clone(),
            |inputs| {
                let v: Vec<String> = inputs.into_iter().map(|s| s.to_owned()).collect();
                black_box(v);
            },
            BatchSize::SmallInput,
        )
    });

    g.bench_function("to_string", |b| {
        b.iter_batched(
            || data.clone(),
            |inputs| {
                let v: Vec<String> = inputs.into_iter().map(|s| s.to_string()).collect();
                black_box(v);
            },
            BatchSize::SmallInput,
        )
    });

    g.bench_function("Cow::<str>::from + into_owned", |b| {
        b.iter_batched(
            || data.clone(),
            |inputs| {
                let v: Vec<String> = inputs
                    .into_iter()
                    .map(|s| Cow::from(s).into_owned())
                    .collect();
                black_box(v);
            },
            BatchSize::SmallInput,
        )
    });

    g.finish();
}

criterion_group!(benches, bench_to_owned);
criterion_main!(benches);

评论区

写评论
xiaoyaou 2025-09-03 15:53

确实,使用rustc生成asm汇编指令,对比str::to_owned()str::to_string()<String as From<&str>>::from()Cow::<'_, str>::into_owned()四个函数的指令,除了个别伪指令标记的区别,其他 cpu指令都是完全一样的

CrazyboyQCD 2025-09-03 09:26

没有太大意义,差异应该是噪音,to_string最后还是调用to_owned,汇编也直接等同于to_owned(https://godbolt.org/z/ajPaqxbGe)

<&str as ToString>::to_string()

->

impl<T: fmt::Display + ?Sized> ToString for T {
    #[inline]
    fn to_string(&self) -> String {
        <Self as SpecToString>::spec_to_string(self)
    }
}

->

impl SpecToString for &str {
    #[inline]
    fn spec_to_string(&self) -> String {
        let s: &str = self;
        String::from(s)
    }
}

->

impl From<&str> for String {
    /// Converts a `&str` into a [`String`].
    ///
    /// The result is allocated on the heap.
    #[inline]
    fn from(s: &str) -> String {
        s.to_owned()
    }
}
作者 vsylva 2025-09-02 21:04
Gnuplot not found, using plotters backend
str_clone_methods/to_owned
                        time:   [438.03 ns 441.71 ns 445.21 ns]
                        change: [+10.395% +14.845% +19.226%] (p = 0.00 < 0.05)
                        Performance has regressed.
str_clone_methods/to_string
                        time:   [402.02 ns 416.75 ns 429.86 ns]
                        change: [−1.1810% +3.1706% +7.4372%] (p = 0.13 > 0.05)
                        No change in performance detected.

Benchmarking str_clone_methods/Cow::<str>::from + into_owned: Collecting 100 samples in estimated 5.0010 s (9.8M iteratistr_clone_methods/Cow::<str>::from + into_owned
                        time:   [374.50 ns 389.25 ns 403.90 ns]
                        change: [−6.1136% −2.5925% +0.9123%] (p = 0.16 > 0.05)
                        No change in performance detected.

---

Gnuplot not found, using plotters backend
str_clone_methods/to_owned
                        time:   [409.68 ns 418.68 ns 426.24 ns]
                        change: [−4.2097% −0.8934% +2.4926%] (p = 0.61 > 0.05)
                        No change in performance detected.
Found 23 outliers among 100 measurements (23.00%)
  22 (22.00%) low severe
  1 (1.00%) low mild
str_clone_methods/to_string
                        time:   [429.27 ns 432.12 ns 435.69 ns]
                        change: [−0.9042% +2.6767% +6.5401%] (p = 0.16 > 0.05)
                        No change in performance detected.
Benchmarking str_clone_methods/Cow::<str>::from + into_owned: Collecting 100 samples in estimated 5.0025 s (9.2M iteratistr_clone_methods/Cow::<str>::from + into_owned
                        time:   [390.76 ns 401.89 ns 410.80 ns]
                        change: [−11.456% −8.0321% −4.0351%] (p = 0.00 < 0.05)
                        Performance has improved.

---

Gnuplot not found, using plotters backend
str_clone_methods/to_owned
                        time:   [326.02 ns 335.87 ns 347.50 ns]
                        change: [−12.854% −9.9655% −6.9592%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 10 outliers among 100 measurements (10.00%)
  1 (1.00%) low mild
  3 (3.00%) high mild
  6 (6.00%) high severe
str_clone_methods/to_string
                        time:   [321.65 ns 322.35 ns 323.19 ns]
                        change: [−1.2680% −0.2993% +0.5430%] (p = 0.57 > 0.05)
                        No change in performance detected.
Found 14 outliers among 100 measurements (14.00%)
  1 (1.00%) low mild
  8 (8.00%) high mild
  5 (5.00%) high severe
Benchmarking str_clone_methods/Cow::<str>::from + into_owned: Collecting 100 samples in estimated 5.0015 s (10M iteratiostr_clone_methods/Cow::<str>::from + into_owned
                        time:   [431.81 ns 432.54 ns 433.36 ns]
                        change: [+4.5968% +6.8659% +9.3458%] (p = 0.00 < 0.05)
                        Performance has regressed.
Found 6 outliers among 100 measurements (6.00%)
  1 (1.00%) high mild
  5 (5.00%) high severe

移动5800h

--
👇
lxl66566: 啥设备 贴下结果?

str_clone_methods/to_owned [505.60 ns 520.51 ns 536.14 ns] str_clone_methods/to_string [513.66 ns 541.82 ns 576.73 ns] str_clone_methods/Cow::::from + into_owned [500.66 ns 516.39 ns 532.23 ns]

lxl66566 2025-09-02 20:54

啥设备 贴下结果?

str_clone_methods/to_owned [505.60 ns 520.51 ns 536.14 ns] str_clone_methods/to_string [513.66 ns 541.82 ns 576.73 ns] str_clone_methods/Cow::::from + into_owned [500.66 ns 516.39 ns 532.23 ns]

1 共 4 条评论, 1 页