Skip to content

Commit 87b2101

Browse files
committed
refactor(sort/benches): optimize locale UTF8 benchmarks by predefining args
Move output file creation and argument setup outside benchmark loops in sort_locale_utf8_bench.rs to avoid measuring initialization time in each iteration, ensuring accurate performance measurements for sorting operations.
1 parent 1ffc813 commit 87b2101

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

src/uu/sort/benches/sort_locale_utf8_bench.rs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@ fn sort_ascii_utf8_locale(bencher: Bencher) {
2121
let output_file = NamedTempFile::new().unwrap();
2222
let output_path = output_file.path().to_str().unwrap().to_string();
2323

24+
let args = ["-o", &output_path, file_path.to_str().unwrap()];
25+
black_box(run_util_function(uumain, &args));
2426
bencher.bench(|| {
25-
black_box(run_util_function(
26-
uumain,
27-
&["-o", &output_path, file_path.to_str().unwrap()],
28-
));
27+
black_box(run_util_function(uumain, &args));
2928
});
3029
}
3130

@@ -37,11 +36,10 @@ fn sort_mixed_utf8_locale(bencher: Bencher) {
3736
let output_file = NamedTempFile::new().unwrap();
3837
let output_path = output_file.path().to_str().unwrap().to_string();
3938

39+
let args = ["-o", &output_path, file_path.to_str().unwrap()];
40+
black_box(run_util_function(uumain, &args));
4041
bencher.bench(|| {
41-
black_box(run_util_function(
42-
uumain,
43-
&["-o", &output_path, file_path.to_str().unwrap()],
44-
));
42+
black_box(run_util_function(uumain, &args));
4543
});
4644
}
4745

@@ -54,12 +52,13 @@ fn sort_numeric_utf8_locale(bencher: Bencher) {
5452
data.extend_from_slice(line.as_bytes());
5553
}
5654
let file_path = setup_test_file(&data);
55+
let output_file = NamedTempFile::new().unwrap();
56+
let output_path = output_file.path().to_str().unwrap().to_string();
5757

58+
let args = ["-n", "-o", &output_path, file_path.to_str().unwrap()];
59+
black_box(run_util_function(uumain, &args));
5860
bencher.bench(|| {
59-
black_box(run_util_function(
60-
uumain,
61-
&["-n", file_path.to_str().unwrap()],
62-
));
61+
black_box(run_util_function(uumain, &args));
6362
});
6463
}
6564

@@ -68,12 +67,13 @@ fn sort_numeric_utf8_locale(bencher: Bencher) {
6867
fn sort_reverse_utf8_locale(bencher: Bencher) {
6968
let data = text_data::generate_mixed_locale_data(50_000);
7069
let file_path = setup_test_file(&data);
70+
let output_file = NamedTempFile::new().unwrap();
71+
let output_path = output_file.path().to_str().unwrap().to_string();
7172

73+
let args = ["-r", "-o", &output_path, file_path.to_str().unwrap()];
74+
black_box(run_util_function(uumain, &args));
7275
bencher.bench(|| {
73-
black_box(run_util_function(
74-
uumain,
75-
&["-r", file_path.to_str().unwrap()],
76-
));
76+
black_box(run_util_function(uumain, &args));
7777
});
7878
}
7979

@@ -82,12 +82,13 @@ fn sort_reverse_utf8_locale(bencher: Bencher) {
8282
fn sort_unique_utf8_locale(bencher: Bencher) {
8383
let data = text_data::generate_mixed_locale_data(50_000);
8484
let file_path = setup_test_file(&data);
85+
let output_file = NamedTempFile::new().unwrap();
86+
let output_path = output_file.path().to_str().unwrap().to_string();
8587

88+
let args = ["-u", "-o", &output_path, file_path.to_str().unwrap()];
89+
black_box(run_util_function(uumain, &args));
8690
bencher.bench(|| {
87-
black_box(run_util_function(
88-
uumain,
89-
&["-u", file_path.to_str().unwrap()],
90-
));
91+
black_box(run_util_function(uumain, &args));
9192
});
9293
}
9394

0 commit comments

Comments
 (0)