Skip to content

Commit 0dc648f

Browse files
Add benchmark
1 parent c63086b commit 0dc648f

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use criterion::{criterion_group, criterion_main, Criterion};
2+
use pyo3::types::PyString;
3+
use pyo3::Python;
4+
use std::hint::black_box;
5+
6+
fn bench_pystring_from_fmt(c: &mut Criterion) {
7+
Python::attach(|py| {
8+
c.bench_function("PyString::from_fmt simple", |b| {
9+
b.iter(|| {
10+
let format_args = format_args!("Hello {}!", "world");
11+
let s = Ok(PyString::new(py, &format!("{format_args}")));
12+
assert!(s.is_ok());
13+
})
14+
});
15+
c.bench_function("PyString::from_fmt complex", |b| {
16+
b.iter(|| {
17+
let value = (black_box(42), black_box("foo"), [0; 0]);
18+
let format_args = format_args!("This is some complex value: {value:?}");
19+
let s = Ok(PyString::new(py, &format!("{format_args}")));
20+
assert!(s.is_ok());
21+
})
22+
});
23+
});
24+
}
25+
26+
criterion_group!(benches, bench_pystring_from_fmt);
27+
criterion_main!(benches);

0 commit comments

Comments
 (0)