Skip to content

Commit

Permalink
[utf-16] Add UTF-16 versions of benches.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfkthame committed Oct 23, 2023
1 parent c80bdf5 commit 94ba2ab
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 2 deletions.
50 changes: 49 additions & 1 deletion benches/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ extern crate unicode_bidi;

use test::Bencher;

use unicode_bidi::BidiInfo;
use unicode_bidi::{BidiInfo, BidiInfoU16};

fn to_utf16(s: &str) -> Vec<u16> {
s.encode_utf16().collect()
}

const LTR_TEXTS: &[&str] = &["abc\ndef\nghi", "abc 123\ndef 456\nghi 789"];

Expand All @@ -29,6 +33,14 @@ fn bench_bidi_info_new(b: &mut Bencher, texts: &[&str]) {
}
}

fn bench_bidi_info_new_u16(b: &mut Bencher, texts: &Vec<Vec<u16>>) {
for text in texts {
b.iter(|| {
BidiInfoU16::new(&text, None);
});
}
}

fn bench_reorder_line(b: &mut Bencher, texts: &[&str]) {
for text in texts {
let bidi_info = BidiInfo::new(text, None);
Expand All @@ -41,6 +53,18 @@ fn bench_reorder_line(b: &mut Bencher, texts: &[&str]) {
}
}

fn bench_reorder_line_u16(b: &mut Bencher, texts: &Vec<Vec<u16>>) {
for text in texts {
let bidi_info = BidiInfoU16::new(text, None);
b.iter(|| {
for para in &bidi_info.paragraphs {
let line = para.range.clone();
bidi_info.reorder_line(para, line);
}
});
}
}

#[bench]
fn bench_1_bidi_info_new_for_ltr_texts(b: &mut Bencher) {
bench_bidi_info_new(b, LTR_TEXTS);
Expand All @@ -60,3 +84,27 @@ fn bench_3_reorder_line_for_ltr_texts(b: &mut Bencher) {
fn bench_4_reorder_line_for_bidi_texts(b: &mut Bencher) {
bench_reorder_line(b, BIDI_TEXTS);
}

#[bench]
fn bench_5_bidi_info_new_for_ltr_texts_u16(b: &mut Bencher) {
let texts_u16: Vec<_> = LTR_TEXTS.iter().map(|t| to_utf16(t)).collect();
bench_bidi_info_new_u16(b, &texts_u16);
}

#[bench]
fn bench_6_bidi_info_new_for_bidi_texts_u16(b: &mut Bencher) {
let texts_u16: Vec<_> = BIDI_TEXTS.iter().map(|t| to_utf16(t)).collect();
bench_bidi_info_new_u16(b, &texts_u16);
}

#[bench]
fn bench_7_reorder_line_for_ltr_texts_u16(b: &mut Bencher) {
let texts_u16: Vec<_> = LTR_TEXTS.iter().map(|t| to_utf16(t)).collect();
bench_reorder_line_u16(b, &texts_u16);
}

#[bench]
fn bench_8_reorder_line_for_bidi_texts_u16(b: &mut Bencher) {
let texts_u16: Vec<_> = BIDI_TEXTS.iter().map(|t| to_utf16(t)).collect();
bench_reorder_line_u16(b, &texts_u16);
}
50 changes: 49 additions & 1 deletion benches/udhr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ extern crate unicode_bidi;

use test::Bencher;

use unicode_bidi::BidiInfo;
use unicode_bidi::{BidiInfo, BidiInfoU16};

fn to_utf16(s: &str) -> Vec<u16> {
s.encode_utf16().collect()
}

const LTR_TEXTS: &[&str] = &[
include_str!("../data/udhr/ltr/udhr_acu_1.txt"),
Expand Down Expand Up @@ -55,6 +59,14 @@ fn bench_bidi_info_new(b: &mut Bencher, texts: &[&str]) {
}
}

fn bench_bidi_info_new_u16(b: &mut Bencher, texts: &Vec<Vec<u16>>) {
for text in texts {
b.iter(|| {
BidiInfoU16::new(&text, None);
});
}
}

fn bench_reorder_line(b: &mut Bencher, texts: &[&str]) {
for text in texts {
let bidi_info = BidiInfo::new(text, None);
Expand All @@ -67,6 +79,18 @@ fn bench_reorder_line(b: &mut Bencher, texts: &[&str]) {
}
}

fn bench_reorder_line_u16(b: &mut Bencher, texts: &Vec<Vec<u16>>) {
for text in texts {
let bidi_info = BidiInfoU16::new(text, None);
b.iter(|| {
for para in &bidi_info.paragraphs {
let line = para.range.clone();
bidi_info.reorder_line(para, line);
}
});
}
}

#[bench]
fn bench_1_bidi_info_new_for_ltr_texts(b: &mut Bencher) {
bench_bidi_info_new(b, LTR_TEXTS);
Expand All @@ -86,3 +110,27 @@ fn bench_3_reorder_line_for_ltr_texts(b: &mut Bencher) {
fn bench_4_reorder_line_for_bidi_texts(b: &mut Bencher) {
bench_reorder_line(b, BIDI_TEXTS);
}

#[bench]
fn bench_5_bidi_info_new_for_ltr_texts_u16(b: &mut Bencher) {
let texts_u16: Vec<_> = LTR_TEXTS.iter().map(|t| to_utf16(t)).collect();
bench_bidi_info_new_u16(b, &texts_u16);
}

#[bench]
fn bench_6_bidi_info_new_for_bidi_texts_u16(b: &mut Bencher) {
let texts_u16: Vec<_> = BIDI_TEXTS.iter().map(|t| to_utf16(t)).collect();
bench_bidi_info_new_u16(b, &texts_u16);
}

#[bench]
fn bench_7_reorder_line_for_ltr_texts_u16(b: &mut Bencher) {
let texts_u16: Vec<_> = LTR_TEXTS.iter().map(|t| to_utf16(t)).collect();
bench_reorder_line_u16(b, &texts_u16);
}

#[bench]
fn bench_8_reorder_line_for_bidi_texts_u16(b: &mut Bencher) {
let texts_u16: Vec<_> = BIDI_TEXTS.iter().map(|t| to_utf16(t)).collect();
bench_reorder_line_u16(b, &texts_u16);
}

0 comments on commit 94ba2ab

Please sign in to comment.