-
Notifications
You must be signed in to change notification settings - Fork 796
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1609 from davidhewitt/bench-gil
benches: add bench_gil
- Loading branch information
Showing
2 changed files
with
47 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use criterion::{criterion_group, criterion_main, BatchSize, Bencher, Criterion}; | ||
|
||
use pyo3::{prelude::*, GILPool}; | ||
|
||
fn bench_clean_gilpool_new(b: &mut Bencher) { | ||
Python::with_gil(|_py| { | ||
b.iter(|| { | ||
let _ = unsafe { GILPool::new() }; | ||
}); | ||
}); | ||
} | ||
|
||
fn bench_clean_acquire_gil(b: &mut Bencher) { | ||
// Acquiring first GIL will also create a "clean" GILPool, so this measures the Python overhead. | ||
b.iter(|| { | ||
let _ = Python::acquire_gil(); | ||
}); | ||
} | ||
|
||
fn bench_dirty_acquire_gil(b: &mut Bencher) { | ||
let obj = Python::with_gil(|py| py.None()); | ||
b.iter_batched( | ||
|| { | ||
// Clone and drop an object so that the GILPool has work to do. | ||
let _ = obj.clone(); | ||
}, | ||
|_| { | ||
let _ = Python::acquire_gil(); | ||
}, | ||
BatchSize::NumBatches(1), | ||
); | ||
} | ||
|
||
fn criterion_benchmark(c: &mut Criterion) { | ||
c.bench_function("clean_gilpool_new", bench_clean_gilpool_new); | ||
c.bench_function("clean_acquire_gil", bench_clean_acquire_gil); | ||
c.bench_function("dirty_acquire_gil", bench_dirty_acquire_gil); | ||
} | ||
|
||
criterion_group!(benches, criterion_benchmark); | ||
criterion_main!(benches); |