From 185e517e96fea7d60532d75af81acb3ef2104bb5 Mon Sep 17 00:00:00 2001 From: sim1222 Date: Sun, 11 Feb 2024 12:48:00 +0000 Subject: [PATCH] no constant folding --- benches/my_benchmark.rs | 11 +++++++---- src/lib.rs | 21 +++++++++------------ 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/benches/my_benchmark.rs b/benches/my_benchmark.rs index d333cca..4148891 100644 --- a/benches/my_benchmark.rs +++ b/benches/my_benchmark.rs @@ -2,12 +2,15 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion}; use rust_benchmark::{concat_with_format, concat_with_plus}; +const A: &str = "post"; +const B: &str = "/fasjhiofasihofas"; + pub fn criterion_benchmark(c: &mut Criterion) { - c.bench_function("concat with format", |b| { - b.iter(|| concat_with_format()); - }); c.bench_function("concat with plus", |b| { - b.iter(|| concat_with_plus()); + b.iter(|| concat_with_plus(black_box(A), black_box(B))); + }); + c.bench_function("concat with format", |b| { + b.iter(|| concat_with_format(black_box(A), black_box(B))); }); } diff --git a/src/lib.rs b/src/lib.rs index a51bb85..385f21e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,20 +1,17 @@ -const A: &str = "post"; -const B: &str = "/fasjhiofasihofas"; +pub fn concat_with_format(a: &str, b: &str) -> String { + let c = format!("{} {}", a, b); + c +} + +pub fn concat_with_plus(a: &str, b: &str) -> String { + let c = a.to_string() + " " + b; + c +} pub fn add(left: usize, right: usize) -> usize { left + right } -pub fn concat_with_plus() -> String { - let c = A.to_string() + " " + B; - c -} - -pub fn concat_with_format() -> String { - let c = format!("{} {}", A, B); - c -} - #[cfg(test)] mod tests { use super::*;