|
| 1 | +use std::hint::black_box; |
| 2 | + |
| 3 | +use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput}; |
| 4 | +use numpy::{PyArrayLike1, PyArrayLike2, PyArrayLike3}; |
| 5 | +use pyo3::{ |
| 6 | + ffi::c_str, |
| 7 | + types::{PyAnyMethods, PyDict}, |
| 8 | + Python, |
| 9 | +}; |
| 10 | + |
| 11 | +fn extract_array_like_1(c: &mut Criterion) { |
| 12 | + const SIZES: &[usize] = &[2_usize.pow(5), 2_usize.pow(10), 2_usize.pow(15)]; |
| 13 | + |
| 14 | + let mut group = c.benchmark_group("extract_array_like_1"); |
| 15 | + for &size in SIZES { |
| 16 | + Python::attach(|py| { |
| 17 | + let locals = PyDict::new(py); |
| 18 | + locals.set_item("size", size).unwrap(); |
| 19 | + |
| 20 | + let list = py |
| 21 | + .eval( |
| 22 | + c_str!("[float(i) for i in range(size)]"), |
| 23 | + Some(&locals), |
| 24 | + None, |
| 25 | + ) |
| 26 | + .unwrap(); |
| 27 | + |
| 28 | + group.throughput(Throughput::Elements(size as u64)); |
| 29 | + group.bench_with_input(BenchmarkId::from_parameter(size), &size, |b, _size| { |
| 30 | + b.iter(|| { |
| 31 | + let list = black_box(&list); |
| 32 | + |
| 33 | + let _array: PyArrayLike1<'_, f64> = black_box(list.extract().unwrap()); |
| 34 | + }); |
| 35 | + }); |
| 36 | + }); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +fn extract_array_like_2(c: &mut Criterion) { |
| 41 | + const SIZES: &[usize] = &[2_usize.pow(3), 2_usize.pow(5), 2_usize.pow(8)]; |
| 42 | + |
| 43 | + let mut group = c.benchmark_group("extract_array_like_2"); |
| 44 | + for &size in SIZES { |
| 45 | + Python::attach(|py| { |
| 46 | + let locals = PyDict::new(py); |
| 47 | + locals.set_item("size", size).unwrap(); |
| 48 | + |
| 49 | + let list = py |
| 50 | + .eval( |
| 51 | + c_str!("[[float(i + j) for i in range(size)] for j in range(size)]"), |
| 52 | + Some(&locals), |
| 53 | + None, |
| 54 | + ) |
| 55 | + .unwrap(); |
| 56 | + |
| 57 | + group.throughput(Throughput::Elements(size.pow(2) as u64)); |
| 58 | + group.bench_with_input(BenchmarkId::from_parameter(size), &size, |b, _size| { |
| 59 | + b.iter(|| { |
| 60 | + let list = black_box(&list); |
| 61 | + |
| 62 | + let _array: PyArrayLike2<'_, f64> = black_box(list.extract().unwrap()); |
| 63 | + }); |
| 64 | + }); |
| 65 | + }); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +fn extract_array_like_3(c: &mut Criterion) { |
| 70 | + const SIZES: &[usize] = &[2_usize.pow(2), 2_usize.pow(4), 2_usize.pow(5)]; |
| 71 | + |
| 72 | + let mut group = c.benchmark_group("extract_array_like_3"); |
| 73 | + for &size in SIZES { |
| 74 | + Python::attach(|py| { |
| 75 | + let locals = PyDict::new(py); |
| 76 | + locals.set_item("size", size).unwrap(); |
| 77 | + |
| 78 | + let list = py |
| 79 | + .eval( |
| 80 | + c_str!("[[[float(i + j + k) for i in range(size)] for j in range(size)] for k in range(size)]"), |
| 81 | + Some(&locals), |
| 82 | + None, |
| 83 | + ) |
| 84 | + .unwrap(); |
| 85 | + |
| 86 | + group.throughput(Throughput::Elements(size.pow(3) as u64)); |
| 87 | + group.bench_with_input(BenchmarkId::from_parameter(size), &size, |b, _size| { |
| 88 | + b.iter(|| { |
| 89 | + let list = black_box(&list); |
| 90 | + |
| 91 | + let _array: PyArrayLike3<'_, f64> = black_box(list.extract().unwrap()); |
| 92 | + }); |
| 93 | + }); |
| 94 | + }); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +criterion_group!( |
| 99 | + benches, |
| 100 | + extract_array_like_1, |
| 101 | + extract_array_like_2, |
| 102 | + extract_array_like_3 |
| 103 | +); |
| 104 | +criterion_main!(benches); |
0 commit comments