|
| 1 | +extern crate criterion; |
| 2 | +extern crate unicode_xid; |
| 3 | + |
| 4 | +use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput}; |
| 5 | +use unicode_xid::UnicodeXID; |
| 6 | + |
| 7 | +fn bench_unicode_xid(c: &mut Criterion) { |
| 8 | + let unicode_chars = chars(1..0x3000); |
| 9 | + let ascii_chars = chars(1..0x80); |
| 10 | + |
| 11 | + let mut group = c.benchmark_group("UnicodeXID"); |
| 12 | + group.throughput(Throughput::Bytes(unicode_chars.len() as u64)); |
| 13 | + group.bench_with_input( |
| 14 | + BenchmarkId::new("is_xid_start", "unicode"), |
| 15 | + &unicode_chars, |
| 16 | + |b, chars| b.iter(|| chars.iter().copied().map(UnicodeXID::is_xid_start).last()), |
| 17 | + ); |
| 18 | + group.throughput(Throughput::Bytes(ascii_chars.len() as u64)); |
| 19 | + group.bench_with_input( |
| 20 | + BenchmarkId::new("is_xid_start", "ascii"), |
| 21 | + &ascii_chars, |
| 22 | + |b, chars| b.iter(|| chars.iter().copied().map(UnicodeXID::is_xid_start).last()), |
| 23 | + ); |
| 24 | + group.throughput(Throughput::Bytes(unicode_chars.len() as u64)); |
| 25 | + group.bench_with_input( |
| 26 | + BenchmarkId::new("is_xid_continue", "unicode"), |
| 27 | + &unicode_chars, |
| 28 | + |b, chars| { |
| 29 | + b.iter(|| { |
| 30 | + chars |
| 31 | + .iter() |
| 32 | + .copied() |
| 33 | + .map(UnicodeXID::is_xid_continue) |
| 34 | + .last() |
| 35 | + }) |
| 36 | + }, |
| 37 | + ); |
| 38 | + group.throughput(Throughput::Bytes(ascii_chars.len() as u64)); |
| 39 | + group.bench_with_input( |
| 40 | + BenchmarkId::new("is_xid_continue", "ascii"), |
| 41 | + &ascii_chars, |
| 42 | + |b, chars| { |
| 43 | + b.iter(|| { |
| 44 | + chars |
| 45 | + .iter() |
| 46 | + .copied() |
| 47 | + .map(UnicodeXID::is_xid_continue) |
| 48 | + .last() |
| 49 | + }) |
| 50 | + }, |
| 51 | + ); |
| 52 | + group.finish(); |
| 53 | +} |
| 54 | + |
| 55 | +fn chars(range: std::ops::Range<u32>) -> Vec<char> { |
| 56 | + range.filter_map(|i| std::char::from_u32(i)).collect() |
| 57 | +} |
| 58 | + |
| 59 | +criterion_group!(benches, bench_unicode_xid); |
| 60 | +criterion_main!(benches); |
0 commit comments