bench/punctperf/punctperf_bench_test.mbt1///|2/// One code point per 97 scalar values, which keeps the sample across all3/// planes and gives a realistic mix of punctuation and non-punctuation.4fn corpus() -> Array[Char] {5 let chars : Array[Char] = []6 for cp in 0..<=0x10FFFF {7 if cp % 97 == 0 {8 chars.push(cp.unsafe_to_char())9 }10 }11 chars12}1314///|15test "punctuation predicate vs general category" (b : @bench.T) {16 let cases : Array[(String, Char)] = [17 ("ascii-punct", '!'),18 ("ascii-letter", 'A'),19 ("ascii-digit", '0'),20 ("space", ' '),21 ("cjk-punct", '\u{3002}'),22 ("cjk-letter", '\u{4E2D}'),23 ("combining-mark", '\u{0301}'),24 ("emoji", '\u{1F600}'),25 ]26 for (label, char) in cases {27 let mut c = char28 b.bench(name="is_punctuation/\{label}", fn() {29 c = char30 b.keep(@unicode.is_punctuation(c))31 })32 b.bench(name="general_category/\{label}", fn() {33 c = char34 b.keep(35 @unicode.general_category(c).group() is @unicode.GeneralCategoryGroup::P,36 )37 })38 }39 let chars = corpus()40 b.bench(name="is_punctuation/corpus-\{chars.length()}", fn() {41 let mut found = 042 for c in chars {43 if @unicode.is_punctuation(c) {44 found += 145 }46 }47 b.keep(found)48 })49 b.bench(name="general_category/corpus-\{chars.length()}", fn() {50 let mut found = 051 for c in chars {52 if @unicode.general_category(c).group()53 is @unicode.GeneralCategoryGroup::P {54 found += 155 }56 }57 b.keep(found)58 })59}60
///|
/// One code point per 97 scalar values, which keeps the sample across all
/// planes and gives a realistic mix of punctuation and non-punctuation.
fn corpus() -> Array[Char] {
let chars : Array[Char] = []
for cp in 0..<=0x10FFFF {
if cp % 97 == 0 {
chars.push(cp.unsafe_to_char())
}
}
chars
}
///|
test "punctuation predicate vs general category" (b : @bench.T) {
let cases : Array[(String, Char)] = [
("ascii-punct", '!'),
("ascii-letter", 'A'),
("ascii-digit", '0'),
("space", ' '),
("cjk-punct", '\u{3002}'),
("cjk-letter", '\u{4E2D}'),
("combining-mark", '\u{0301}'),
("emoji", '\u{1F600}'),
]
for (label, char) in cases {
let mut c = char
b.bench(name="is_punctuation/\{label}", fn() {
c = char
b.keep(@unicode.is_punctuation(c))
})
b.bench(name="general_category/\{label}", fn() {
c = char
b.keep(
@unicode.general_category(c).group() is @unicode.GeneralCategoryGroup::P,
)
})
}
let chars = corpus()
b.bench(name="is_punctuation/corpus-\{chars.length()}", fn() {
let mut found = 0
for c in chars {
if @unicode.is_punctuation(c) {
found += 1
}
}
b.keep(found)
})
b.bench(name="general_category/corpus-\{chars.length()}", fn() {
let mut found = 0
for c in chars {
if @unicode.general_category(c).group()
is @unicode.GeneralCategoryGroup::P {
found += 1
}
}
b.keep(found)
})
}