Skip to content

Commit fc95e0e

Browse files
committed
feat: implement benchmarks for TheAlgorithms (backtracking, big_interger, bit_manipulation)
1 parent 1ff6bcf commit fc95e0e

File tree

9 files changed

+225
-1
lines changed

9 files changed

+225
-1
lines changed

.github/workflows/ci.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ jobs:
1010
runs-on: ubuntu-latest
1111
steps:
1212
- uses: actions/checkout@v4
13+
with:
14+
submodules: true
1315
- uses: moonrepo/setup-rust@v1
1416
with:
1517
components: rustfmt, clippy
@@ -21,6 +23,8 @@ jobs:
2123
runs-on: ubuntu-latest
2224
steps:
2325
- uses: actions/checkout@v4
26+
with:
27+
submodules: true
2428
- uses: moonrepo/setup-rust@v1
2529
with:
2630
bins: cargo-msrv
@@ -35,13 +39,17 @@ jobs:
3539
runs-on: ubuntu-latest
3640
steps:
3741
- uses: actions/checkout@v4
42+
with:
43+
submodules: true
3844
- uses: moonrepo/setup-rust@v1
3945
- run: cargo test --all
4046

4147
compat-integration-test-instrumentation:
4248
runs-on: ubuntu-latest
4349
steps:
4450
- uses: actions/checkout@v4
51+
with:
52+
submodules: true
4553
- uses: moonrepo/setup-rust@v1
4654
with:
4755
cache-target: release
@@ -65,6 +73,8 @@ jobs:
6573
runs-on: codspeed-macro
6674
steps:
6775
- uses: actions/checkout@v4
76+
with:
77+
submodules: true
6878
- uses: moonrepo/setup-rust@v1
6979
with:
7080
cache-target: release

.github/workflows/release.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ jobs:
1414
steps:
1515
- uses: actions/checkout@v4
1616
with:
17+
submodules: true
1718
fetch-depth: 0
1819
- uses: moonrepo/setup-rust@v0
1920
with:

.gitmodules

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
[submodule "crates/divan_compat/examples/src/the_algorithms/Rust"]
3+
path = crates/divan_compat/examples/src/the_algorithms/Rust
4+
url = git@github.com:TheAlgorithms/Rust.git

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/divan_compat/examples/Cargo.toml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,16 @@ description = "Examples for Divan, a comfy benchmarking framework."
66
publish = false
77
license = "MIT OR Apache-2.0"
88

9+
910
[dependencies]
10-
fastrand = "2.3.0"
1111
divan = { package = "codspeed-divan-compat", path = ".." }
12+
fastrand = "2.3.0"
13+
num-bigint = { version = "0.4", optional = true }
14+
num-traits = { version = "0.2", optional = true }
15+
16+
[features]
17+
default = ["big-math"]
18+
big-math = ["num-bigint", "num-traits"]
1219

1320
[[bench]]
1421
name = "math"
@@ -21,3 +28,7 @@ harness = false
2128
[[bench]]
2229
name = "time"
2330
harness = false
31+
32+
[[bench]]
33+
name = "the_algorithms"
34+
harness = false
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
use codspeed_divan_compat_examples::the_algorithms;
2+
use divan::black_box;
3+
4+
fn main() {
5+
divan::main();
6+
}
7+
8+
mod backtracking {
9+
use super::*;
10+
11+
#[divan::bench(args = [4, 5, 6, 7, 8])]
12+
fn n_queens_solver(n: usize) -> Vec<Vec<String>> {
13+
the_algorithms::backtracking::n_queens_solver(n)
14+
}
15+
16+
// Benchmark parentheses generation with different sizes
17+
#[divan::bench(args = [3, 4, 5, 6])]
18+
fn generate_parentheses(n: usize) -> Vec<String> {
19+
the_algorithms::backtracking::generate_parentheses(n)
20+
}
21+
22+
// Benchmark combinations generation with different n values, keeping k=3
23+
#[divan::bench(args = [5, 6, 7, 8, 9])]
24+
fn generate_combinations(n: usize) -> Vec<Vec<usize>> {
25+
the_algorithms::backtracking::generate_all_combinations(n, 3).unwrap()
26+
}
27+
28+
// Benchmark graph coloring with different sizes of complete graphs
29+
#[divan::bench(args = [3, 4, 5, 6])]
30+
fn graph_coloring(bencher: divan::Bencher, n: usize) {
31+
// Create a complete graph of size n (all vertices connected to each other)
32+
let matrix = (0..n)
33+
.map(|i| (0..n).map(|j| i != j).collect())
34+
.collect::<Vec<Vec<bool>>>();
35+
36+
bencher.bench_local(|| {
37+
black_box(the_algorithms::backtracking::generate_colorings(
38+
black_box(matrix.clone()),
39+
3,
40+
))
41+
});
42+
}
43+
44+
// Benchmark Hamiltonian cycle finding with different sizes of cyclic graphs
45+
#[divan::bench(args = [4, 5, 6, 7])]
46+
fn hamiltonian_cycle(bencher: divan::Bencher, n: usize) {
47+
// Create a cyclic graph where each vertex is connected to its neighbors
48+
// This ensures a Hamiltonian cycle exists
49+
let matrix = (0..n)
50+
.map(|i| {
51+
(0..n)
52+
.map(|j| {
53+
let prev = (i + n - 1) % n;
54+
let next = (i + 1) % n;
55+
j == prev || j == next
56+
})
57+
.collect()
58+
})
59+
.collect::<Vec<Vec<bool>>>();
60+
61+
bencher.bench_local(|| {
62+
black_box(the_algorithms::backtracking::find_hamiltonian_cycle(
63+
black_box(matrix.clone()),
64+
0,
65+
))
66+
});
67+
}
68+
69+
// Benchmark Knight's Tour with different board sizes
70+
#[divan::bench(args = [5, 6, 7, 8])]
71+
fn knight_tour(bencher: divan::Bencher, n: usize) {
72+
bencher.bench_local(|| {
73+
black_box(the_algorithms::backtracking::find_knight_tour(
74+
black_box(n),
75+
black_box(n),
76+
black_box(0),
77+
black_box(0),
78+
))
79+
});
80+
}
81+
82+
// Benchmark permutations with different input sizes
83+
#[divan::bench(args = [3, 4, 5, 6, 7])]
84+
fn permutations(bencher: divan::Bencher, n: usize) {
85+
let nums: Vec<isize> = (0..n).map(|x| x as isize).collect();
86+
87+
bencher.bench_local(|| {
88+
black_box(the_algorithms::backtracking::permute(black_box(
89+
nums.clone(),
90+
)))
91+
});
92+
}
93+
94+
// Benchmark Rat in Maze with different maze sizes
95+
#[divan::bench(args = [5, 6, 7, 8])]
96+
fn rat_in_maze(bencher: divan::Bencher, n: usize) {
97+
// Create a maze where the rat can move diagonally to the end
98+
let maze = (0..n)
99+
.map(|i| (0..n).map(|j| i == j || i == j + 1).collect())
100+
.collect::<Vec<Vec<bool>>>();
101+
102+
bencher.bench_local(|| {
103+
black_box(the_algorithms::backtracking::find_path_in_maze(
104+
black_box(&maze),
105+
black_box(0),
106+
black_box(0),
107+
))
108+
});
109+
}
110+
111+
// Benchmark Subset Sum with different set sizes
112+
#[divan::bench(args = [10, 12, 14, 16, 18])]
113+
fn subset_sum(bencher: divan::Bencher, n: usize) {
114+
let set: Vec<isize> = (0..n).map(|x| x as isize).collect();
115+
let target = (n as isize) * 2; // A challenging but achievable target
116+
117+
bencher.bench_local(|| {
118+
black_box(the_algorithms::backtracking::has_subset_with_sum(
119+
black_box(&set),
120+
black_box(target),
121+
))
122+
});
123+
}
124+
125+
// Benchmark Sudoku solver with different levels of difficulty
126+
#[divan::bench]
127+
fn sudoku(bencher: divan::Bencher) {
128+
// A moderately difficult Sudoku puzzle
129+
let board = [
130+
[3, 0, 6, 5, 0, 8, 4, 0, 0],
131+
[5, 2, 0, 0, 0, 0, 0, 0, 0],
132+
[0, 8, 7, 0, 0, 0, 0, 3, 1],
133+
[0, 0, 3, 0, 1, 0, 0, 8, 0],
134+
[9, 0, 0, 8, 6, 3, 0, 0, 5],
135+
[0, 5, 0, 0, 9, 0, 6, 0, 0],
136+
[1, 3, 0, 0, 0, 0, 2, 5, 0],
137+
[0, 0, 0, 0, 0, 0, 0, 7, 4],
138+
[0, 0, 5, 2, 0, 6, 3, 0, 0],
139+
];
140+
141+
bencher.bench_local(|| {
142+
black_box(the_algorithms::backtracking::sudoku_solver(black_box(
143+
&board,
144+
)))
145+
});
146+
}
147+
}
148+
149+
mod bit_manipulation {
150+
use super::*;
151+
152+
#[divan::bench(args = [0, 42, 255, 1024, 65535])]
153+
fn count_set_bits(bencher: divan::Bencher, n: u32) {
154+
bencher.bench_local(|| {
155+
black_box(the_algorithms::bit_manipulation::count_set_bits(black_box(
156+
n.try_into().unwrap(),
157+
)))
158+
});
159+
}
160+
161+
#[divan::bench(args = [0, 42, 255, 1024, 65535])]
162+
fn find_highest_set_bit(bencher: divan::Bencher, n: u32) {
163+
bencher.bench_local(|| {
164+
black_box(the_algorithms::bit_manipulation::find_highest_set_bit(
165+
black_box(n.try_into().unwrap()),
166+
))
167+
});
168+
}
169+
170+
#[divan::bench(args = [1, 2, 3, 4, 5])]
171+
fn generate_gray_code(bencher: divan::Bencher, n: u32) {
172+
bencher.bench_local(|| {
173+
black_box(the_algorithms::bit_manipulation::generate_gray_code(
174+
black_box(n.try_into().unwrap()),
175+
))
176+
});
177+
}
178+
179+
#[divan::bench(args = &[(0, 0), (42, 13), (255, 255), (1024, -1024), (65535, -65535)])]
180+
fn add_two_integers(bencher: divan::Bencher, (a, b): (i32, i32)) {
181+
bencher.bench_local(|| {
182+
black_box(the_algorithms::bit_manipulation::add_two_integers(
183+
black_box(a.try_into().unwrap()),
184+
black_box(b.try_into().unwrap()),
185+
))
186+
});
187+
}
188+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#[allow(clippy::all)]
2+
pub mod the_algorithms;
Submodule Rust added at ae10da6
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#[path = "./Rust/src/bit_manipulation/mod.rs"]
2+
pub mod bit_manipulation;
3+
4+
#[path = "./Rust/src/backtracking/mod.rs"]
5+
pub mod backtracking;

0 commit comments

Comments
 (0)