unicode

Branch: main

Switch branches or tags

Branch: main
Go to file t

413 files indexed

History
Code

bench/punctperf/punctperf_bench_test.mbt
1///|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