From 1c7b7daad8847c47cb475c24136c0669e7a21163 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johanna=20S=C3=B6rng=C3=A5rd?= Date: Mon, 6 Apr 2026 20:41:53 +0200 Subject: [PATCH 1/5] Add benchmarks --- Cargo.lock | 579 ++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 5 + benches/slice_sort.rs | 72 ++++++ 3 files changed, 656 insertions(+) create mode 100644 benches/slice_sort.rs diff --git a/Cargo.lock b/Cargo.lock index 3521f60..a4cb1eb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,22 +2,211 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "aho-corasick" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301" +dependencies = [ + "memchr", +] + +[[package]] +name = "alloca" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5a7d05ea6aea7e9e64d25b9156ba2fee3fdd659e34e41063cd2fc7cd020d7f4" +dependencies = [ + "cc", +] + +[[package]] +name = "anes" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" + +[[package]] +name = "anstyle" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000" + +[[package]] +name = "autocfg" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" + +[[package]] +name = "bumpalo" +version = "3.20.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d20789868f4b01b2f2caec9f5c4e0213b41e3e5702a50157d699ae31ced2fcb" + +[[package]] +name = "cast" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" + +[[package]] +name = "cc" +version = "1.2.59" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7a4d3ec6524d28a329fc53654bbadc9bdd7b0431f5d65f1a56ffb28a1ee5283" +dependencies = [ + "find-msvc-tools", + "shlex", +] + [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "ciborium" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42e69ffd6f0917f5c029256a24d0161db17cea3997d185db0d35926308770f0e" +dependencies = [ + "ciborium-io", + "ciborium-ll", + "serde", +] + +[[package]] +name = "ciborium-io" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05afea1e0a06c9be33d539b876f1ce3692f4afea2cb41f740e7743225ed1c757" + +[[package]] +name = "ciborium-ll" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57663b653d948a338bfb3eeba9bb2fd5fcfaecb9e199e87e1eda4d9e8b240fd9" +dependencies = [ + "ciborium-io", + "half", +] + +[[package]] +name = "clap" +version = "4.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b193af5b67834b676abd72466a96c1024e6a6ad978a1f484bd90b85c94041351" +dependencies = [ + "clap_builder", +] + +[[package]] +name = "clap_builder" +version = "4.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "714a53001bf66416adb0e2ef5ac857140e7dc3a0c48fb28b2f10762fc4b5069f" +dependencies = [ + "anstyle", + "clap_lex", +] + +[[package]] +name = "clap_lex" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8d4a3bb8b1e0c1050499d1815f5ab16d04f0959b233085fb31653fbfc9d98f9" + [[package]] name = "compile_time_sort" version = "1.2.4" dependencies = [ + "criterion", "paste", "quickcheck", "rand", "rustversion", ] +[[package]] +name = "criterion" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "950046b2aa2492f9a536f5f4f9a3de7b9e2476e575e05bd6c333371add4d98f3" +dependencies = [ + "alloca", + "anes", + "cast", + "ciborium", + "clap", + "criterion-plot", + "itertools", + "num-traits", + "oorandom", + "page_size", + "plotters", + "rayon", + "regex", + "serde", + "serde_json", + "tinytemplate", + "walkdir", +] + +[[package]] +name = "criterion-plot" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8d80a2f4f5b554395e47b5d8305bc3d27813bacb73493eb1001e8f76dae29ea" +dependencies = [ + "cast", + "itertools", +] + +[[package]] +name = "crossbeam-deque" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" + +[[package]] +name = "crunchy" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5" + +[[package]] +name = "either" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" + +[[package]] +name = "find-msvc-tools" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582" + [[package]] name = "getrandom" version = "0.2.15" @@ -29,18 +218,128 @@ dependencies = [ "wasi", ] +[[package]] +name = "half" +version = "2.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ea2d84b969582b4b1864a92dc5d27cd2b77b622a8d79306834f1be5ba20d84b" +dependencies = [ + "cfg-if", + "crunchy", + "zerocopy", +] + +[[package]] +name = "itertools" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" +dependencies = [ + "either", +] + +[[package]] +name = "itoa" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" + +[[package]] +name = "js-sys" +version = "0.3.94" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e04e2ef80ce82e13552136fabeef8a5ed1f985a96805761cbb9a2c34e7664d9" +dependencies = [ + "once_cell", + "wasm-bindgen", +] + [[package]] name = "libc" version = "0.2.171" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c19937216e9d3aa9956d9bb8dfc0b0c8beb6058fc4f7a4dc4d850edf86a237d6" +[[package]] +name = "memchr" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79" + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.21.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" + +[[package]] +name = "oorandom" +version = "11.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6790f58c7ff633d8771f42965289203411a5e5c68388703c06e14f24770b41e" + +[[package]] +name = "page_size" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30d5b2194ed13191c1999ae0704b7839fb18384fa22e49b57eeaa97d79ce40da" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "paste" version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" +[[package]] +name = "plotters" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5aeb6f403d7a4911efb1e33402027fc44f29b5bf6def3effcc22d7bb75f2b747" +dependencies = [ + "num-traits", + "plotters-backend", + "plotters-svg", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "plotters-backend" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df42e13c12958a16b3f7f4386b9ab1f3e7933914ecea48da7139435263a4172a" + +[[package]] +name = "plotters-svg" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51bae2ac328883f7acdfea3d66a7c35751187f870bc81f94563733a154d7a670" +dependencies = [ + "plotters-backend", +] + +[[package]] +name = "proc-macro2" +version = "1.0.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" +dependencies = [ + "unicode-ident", +] + [[package]] name = "quickcheck" version = "1.0.3" @@ -50,6 +349,15 @@ dependencies = [ "rand", ] +[[package]] +name = "quote" +version = "1.0.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924" +dependencies = [ + "proc-macro2", +] + [[package]] name = "rand" version = "0.8.5" @@ -68,14 +376,285 @@ dependencies = [ "getrandom", ] +[[package]] +name = "rayon" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "368f01d005bf8fd9b1206fb6fa653e6c4a81ceb1466406b81792d87c5677a58f" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22e18b0f0062d30d4230b2e85ff77fdfe4326feb054b9783a3460d8435c8ab91" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] + +[[package]] +name = "regex" +version = "1.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e10754a14b9137dd7b1e3e5b0493cc9171fdd105e0ab477f51b72e7f3ac0e276" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e1dd4122fc1595e8162618945476892eefca7b88c52820e74af6262213cae8f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc897dd8d9e8bd1ed8cdad82b5966c3e0ecae09fb1907d58efaa013543185d0a" + [[package]] name = "rustversion" version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "serde" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" +dependencies = [ + "serde_core", + "serde_derive", +] + +[[package]] +name = "serde_core" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.149" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83fc039473c5595ace860d8c4fafa220ff474b3fc6bfdb4293327f1a37e94d86" +dependencies = [ + "itoa", + "memchr", + "serde", + "serde_core", + "zmij", +] + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + +[[package]] +name = "syn" +version = "2.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "tinytemplate" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc" +dependencies = [ + "serde", + "serde_json", +] + +[[package]] +name = "unicode-ident" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" + +[[package]] +name = "walkdir" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" +dependencies = [ + "same-file", + "winapi-util", +] + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "wasm-bindgen" +version = "0.2.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0551fc1bb415591e3372d0bc4780db7e587d84e2a7e79da121051c5c4b89d0b0" +dependencies = [ + "cfg-if", + "once_cell", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fbdf9a35adf44786aecd5ff89b4563a90325f9da0923236f6104e603c7e86be" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dca9693ef2bab6d4e6707234500350d8dad079eb508dca05530c85dc3a529ff2" +dependencies = [ + "bumpalo", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39129a682a6d2d841b6c429d0c51e5cb0ed1a03829d8b3d1e69a011e62cb3d3b" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "web-sys" +version = "0.3.94" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd70027e39b12f0849461e08ffc50b9cd7688d942c1c8e3c7b22273236b4dd0a" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] + +[[package]] +name = "zerocopy" +version = "0.8.48" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eed437bf9d6692032087e337407a86f04cd8d6a16a37199ed57949d415bd68e9" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.48" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70e3cd084b1788766f53af483dd21f93881ff30d7320490ec3ef7526d203bad4" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "zmij" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa" diff --git a/Cargo.toml b/Cargo.toml index bb1dedc..66a167a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,3 +18,8 @@ paste = "1.0.15" [dev-dependencies] rand = { version = "0.8.5", default-features = false, features = ["small_rng"] } quickcheck = { version = "1.0", default-features = false } +criterion = "0.8.2" + +[[bench]] +harness = false +name = "slice_sort" \ No newline at end of file diff --git a/benches/slice_sort.rs b/benches/slice_sort.rs new file mode 100644 index 0000000..4ea276c --- /dev/null +++ b/benches/slice_sort.rs @@ -0,0 +1,72 @@ +use compile_time_sort::{sort_f32_slice, sort_i32_slice, sort_u8_slice}; +use criterion::{criterion_group, criterion_main, BatchSize, Criterion}; +use rand::{rngs::SmallRng, Rng, SeedableRng}; + +fn bench_sort_i32(c: &mut Criterion) { + let mut rng = SmallRng::from_seed([42; 32]); + + let data: Vec = (0..1000).map(|_| rng.gen()).collect(); + + c.bench_function("std::sort_unstable_i32", |b| { + b.iter_batched( + || data.clone(), + |mut slice| slice.sort_unstable(), + BatchSize::SmallInput, + ) + }); + + c.bench_function("sort_i32_slice", |b| { + b.iter_batched( + || data.clone(), + |mut slice| sort_i32_slice(&mut slice), + BatchSize::SmallInput, + ) + }); +} + +fn bench_sort_f32(c: &mut Criterion) { + let mut rng = SmallRng::from_seed([42; 32]); + + let data: Vec = (0..1000).map(|_| rng.gen()).collect(); + + c.bench_function("std::sort_unstable_f32", |b| { + b.iter_batched( + || data.clone(), + |mut slice| slice.sort_unstable_by(|a, b| a.total_cmp(b)), + BatchSize::SmallInput, + ) + }); + + c.bench_function("sort_f32_slice", |b| { + b.iter_batched( + || data.clone(), + |mut slice| sort_f32_slice(&mut slice), + BatchSize::SmallInput, + ) + }); +} + +fn bench_sort_u8(c: &mut Criterion) { + let mut rng = SmallRng::from_seed([42; 32]); + + let data: Vec = (0..1000).map(|_| rng.gen()).collect(); + + c.bench_function("std::sort_unstable_u8", |b| { + b.iter_batched( + || data.clone(), + |mut slice| slice.sort_unstable(), + BatchSize::SmallInput, + ) + }); + + c.bench_function("sort_u8_slice", |b| { + b.iter_batched( + || data.clone(), + |mut slice| sort_u8_slice(&mut slice), + BatchSize::SmallInput, + ) + }); +} + +criterion_group!(benches, bench_sort_i32, bench_sort_f32, bench_sort_u8); +criterion_main!(benches); From 9e1bbb9537d987060acda4e71f5e67f2a9946964 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johanna=20S=C3=B6rng=C3=A5rd?= Date: Mon, 6 Apr 2026 20:47:37 +0200 Subject: [PATCH 2/5] Add bool benchmark --- benches/slice_sort.rs | 45 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/benches/slice_sort.rs b/benches/slice_sort.rs index 4ea276c..9c3da66 100644 --- a/benches/slice_sort.rs +++ b/benches/slice_sort.rs @@ -1,4 +1,5 @@ -use compile_time_sort::{sort_f32_slice, sort_i32_slice, sort_u8_slice}; +use compile_time_sort::{sort_bool_slice, sort_f32_slice, sort_i32_slice, sort_u8_slice}; +use core::hint::black_box; use criterion::{criterion_group, criterion_main, BatchSize, Criterion}; use rand::{rngs::SmallRng, Rng, SeedableRng}; @@ -10,7 +11,7 @@ fn bench_sort_i32(c: &mut Criterion) { c.bench_function("std::sort_unstable_i32", |b| { b.iter_batched( || data.clone(), - |mut slice| slice.sort_unstable(), + |mut slice| black_box(slice.sort_unstable()), BatchSize::SmallInput, ) }); @@ -18,7 +19,7 @@ fn bench_sort_i32(c: &mut Criterion) { c.bench_function("sort_i32_slice", |b| { b.iter_batched( || data.clone(), - |mut slice| sort_i32_slice(&mut slice), + |mut slice| black_box(sort_i32_slice(&mut slice)), BatchSize::SmallInput, ) }); @@ -32,7 +33,7 @@ fn bench_sort_f32(c: &mut Criterion) { c.bench_function("std::sort_unstable_f32", |b| { b.iter_batched( || data.clone(), - |mut slice| slice.sort_unstable_by(|a, b| a.total_cmp(b)), + |mut slice| black_box(slice.sort_unstable_by(|a, b| a.total_cmp(b))), BatchSize::SmallInput, ) }); @@ -40,7 +41,7 @@ fn bench_sort_f32(c: &mut Criterion) { c.bench_function("sort_f32_slice", |b| { b.iter_batched( || data.clone(), - |mut slice| sort_f32_slice(&mut slice), + |mut slice| black_box(sort_f32_slice(&mut slice)), BatchSize::SmallInput, ) }); @@ -54,7 +55,7 @@ fn bench_sort_u8(c: &mut Criterion) { c.bench_function("std::sort_unstable_u8", |b| { b.iter_batched( || data.clone(), - |mut slice| slice.sort_unstable(), + |mut slice| black_box(slice.sort_unstable()), BatchSize::SmallInput, ) }); @@ -62,11 +63,39 @@ fn bench_sort_u8(c: &mut Criterion) { c.bench_function("sort_u8_slice", |b| { b.iter_batched( || data.clone(), - |mut slice| sort_u8_slice(&mut slice), + |mut slice| black_box(sort_u8_slice(&mut slice)), BatchSize::SmallInput, ) }); } -criterion_group!(benches, bench_sort_i32, bench_sort_f32, bench_sort_u8); +fn bench_sort_bool(c: &mut Criterion) { + let mut rng = SmallRng::from_seed([42; 32]); + + let data: Vec = (0..1000).map(|_| rng.gen()).collect(); + + c.bench_function("std::sort_unstable_bool", |b| { + b.iter_batched( + || data.clone(), + |mut slice| black_box(slice.sort_unstable()), + BatchSize::SmallInput, + ) + }); + + c.bench_function("sort_bool_slice", |b| { + b.iter_batched( + || data.clone(), + |mut slice| black_box(sort_bool_slice(&mut slice)), + BatchSize::SmallInput, + ) + }); +} + +criterion_group!( + benches, + bench_sort_i32, + bench_sort_f32, + bench_sort_u8, + bench_sort_bool +); criterion_main!(benches); From c6b0bac555be4edc92dedbc46146e3099b09465d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johanna=20S=C3=B6rng=C3=A5rd?= Date: Mon, 6 Apr 2026 20:50:17 +0200 Subject: [PATCH 3/5] Add u128 benchmark --- benches/slice_sort.rs | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/benches/slice_sort.rs b/benches/slice_sort.rs index 9c3da66..128bb72 100644 --- a/benches/slice_sort.rs +++ b/benches/slice_sort.rs @@ -1,4 +1,6 @@ -use compile_time_sort::{sort_bool_slice, sort_f32_slice, sort_i32_slice, sort_u8_slice}; +use compile_time_sort::{ + sort_bool_slice, sort_f32_slice, sort_i32_slice, sort_u128_slice, sort_u8_slice, +}; use core::hint::black_box; use criterion::{criterion_group, criterion_main, BatchSize, Criterion}; use rand::{rngs::SmallRng, Rng, SeedableRng}; @@ -91,11 +93,34 @@ fn bench_sort_bool(c: &mut Criterion) { }); } +fn bench_sort_u128(c: &mut Criterion) { + let mut rng = SmallRng::from_seed([42; 32]); + + let data: Vec = (0..1000).map(|_| rng.gen()).collect(); + + c.bench_function("std::sort_unstable_u128", |b| { + b.iter_batched( + || data.clone(), + |mut slice| black_box(slice.sort_unstable()), + BatchSize::SmallInput, + ) + }); + + c.bench_function("sort_u128_slice", |b| { + b.iter_batched( + || data.clone(), + |mut slice| black_box(sort_u128_slice(&mut slice)), + BatchSize::SmallInput, + ) + }); +} + criterion_group!( benches, bench_sort_i32, bench_sort_f32, bench_sort_u8, - bench_sort_bool + bench_sort_bool, + bench_sort_u128 ); criterion_main!(benches); From 20597d04da5911a7de5af7d7ea6e1dcab51a6ad9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johanna=20S=C3=B6rng=C3=A5rd?= Date: Mon, 6 Apr 2026 20:54:57 +0200 Subject: [PATCH 4/5] Add f64 benchmark --- benches/slice_sort.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/benches/slice_sort.rs b/benches/slice_sort.rs index 128bb72..8c46abb 100644 --- a/benches/slice_sort.rs +++ b/benches/slice_sort.rs @@ -1,5 +1,5 @@ use compile_time_sort::{ - sort_bool_slice, sort_f32_slice, sort_i32_slice, sort_u128_slice, sort_u8_slice, + sort_bool_slice, sort_f32_slice, sort_f64_slice, sort_i32_slice, sort_u128_slice, sort_u8_slice, }; use core::hint::black_box; use criterion::{criterion_group, criterion_main, BatchSize, Criterion}; @@ -115,10 +115,33 @@ fn bench_sort_u128(c: &mut Criterion) { }); } +fn bench_sort_f64(c: &mut Criterion) { + let mut rng = SmallRng::from_seed([42; 32]); + + let data: Vec = (0..1000).map(|_| rng.gen()).collect(); + + c.bench_function("std::sort_unstable_f64", |b| { + b.iter_batched( + || data.clone(), + |mut slice| black_box(slice.sort_unstable_by(|a, b| a.total_cmp(b))), + BatchSize::SmallInput, + ) + }); + + c.bench_function("sort_f64_slice", |b| { + b.iter_batched( + || data.clone(), + |mut slice| black_box(sort_f64_slice(&mut slice)), + BatchSize::SmallInput, + ) + }); +} + criterion_group!( benches, bench_sort_i32, bench_sort_f32, + bench_sort_f64, bench_sort_u8, bench_sort_bool, bench_sort_u128 From 82e1f1319a180cadef40a1f8f1df7088e1094081 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johanna=20S=C3=B6rng=C3=A5rd?= Date: Mon, 6 Apr 2026 20:58:45 +0200 Subject: [PATCH 5/5] Split the std benches into a different file --- Cargo.toml | 4 ++ benches/slice_sort.rs | 48 ------------------ benches/std_unstable_sort.rs | 98 ++++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 48 deletions(-) create mode 100644 benches/std_unstable_sort.rs diff --git a/Cargo.toml b/Cargo.toml index 66a167a..53fb41b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,10 @@ rand = { version = "0.8.5", default-features = false, features = ["small_rng"] } quickcheck = { version = "1.0", default-features = false } criterion = "0.8.2" +[[bench]] +harness = false +name = "std_unstable_sort" + [[bench]] harness = false name = "slice_sort" \ No newline at end of file diff --git a/benches/slice_sort.rs b/benches/slice_sort.rs index 8c46abb..bd3459d 100644 --- a/benches/slice_sort.rs +++ b/benches/slice_sort.rs @@ -10,14 +10,6 @@ fn bench_sort_i32(c: &mut Criterion) { let data: Vec = (0..1000).map(|_| rng.gen()).collect(); - c.bench_function("std::sort_unstable_i32", |b| { - b.iter_batched( - || data.clone(), - |mut slice| black_box(slice.sort_unstable()), - BatchSize::SmallInput, - ) - }); - c.bench_function("sort_i32_slice", |b| { b.iter_batched( || data.clone(), @@ -32,14 +24,6 @@ fn bench_sort_f32(c: &mut Criterion) { let data: Vec = (0..1000).map(|_| rng.gen()).collect(); - c.bench_function("std::sort_unstable_f32", |b| { - b.iter_batched( - || data.clone(), - |mut slice| black_box(slice.sort_unstable_by(|a, b| a.total_cmp(b))), - BatchSize::SmallInput, - ) - }); - c.bench_function("sort_f32_slice", |b| { b.iter_batched( || data.clone(), @@ -54,14 +38,6 @@ fn bench_sort_u8(c: &mut Criterion) { let data: Vec = (0..1000).map(|_| rng.gen()).collect(); - c.bench_function("std::sort_unstable_u8", |b| { - b.iter_batched( - || data.clone(), - |mut slice| black_box(slice.sort_unstable()), - BatchSize::SmallInput, - ) - }); - c.bench_function("sort_u8_slice", |b| { b.iter_batched( || data.clone(), @@ -76,14 +52,6 @@ fn bench_sort_bool(c: &mut Criterion) { let data: Vec = (0..1000).map(|_| rng.gen()).collect(); - c.bench_function("std::sort_unstable_bool", |b| { - b.iter_batched( - || data.clone(), - |mut slice| black_box(slice.sort_unstable()), - BatchSize::SmallInput, - ) - }); - c.bench_function("sort_bool_slice", |b| { b.iter_batched( || data.clone(), @@ -98,14 +66,6 @@ fn bench_sort_u128(c: &mut Criterion) { let data: Vec = (0..1000).map(|_| rng.gen()).collect(); - c.bench_function("std::sort_unstable_u128", |b| { - b.iter_batched( - || data.clone(), - |mut slice| black_box(slice.sort_unstable()), - BatchSize::SmallInput, - ) - }); - c.bench_function("sort_u128_slice", |b| { b.iter_batched( || data.clone(), @@ -120,14 +80,6 @@ fn bench_sort_f64(c: &mut Criterion) { let data: Vec = (0..1000).map(|_| rng.gen()).collect(); - c.bench_function("std::sort_unstable_f64", |b| { - b.iter_batched( - || data.clone(), - |mut slice| black_box(slice.sort_unstable_by(|a, b| a.total_cmp(b))), - BatchSize::SmallInput, - ) - }); - c.bench_function("sort_f64_slice", |b| { b.iter_batched( || data.clone(), diff --git a/benches/std_unstable_sort.rs b/benches/std_unstable_sort.rs new file mode 100644 index 0000000..8ccd8f1 --- /dev/null +++ b/benches/std_unstable_sort.rs @@ -0,0 +1,98 @@ +use core::hint::black_box; +use criterion::{criterion_group, criterion_main, BatchSize, Criterion}; +use rand::{rngs::SmallRng, Rng, SeedableRng}; + +fn bench_sort_i32(c: &mut Criterion) { + let mut rng = SmallRng::from_seed([42; 32]); + + let data: Vec = (0..1000).map(|_| rng.gen()).collect(); + + c.bench_function("std::sort_unstable_i32", |b| { + b.iter_batched( + || data.clone(), + |mut slice| black_box(slice.sort_unstable()), + BatchSize::SmallInput, + ) + }); +} + +fn bench_sort_f32(c: &mut Criterion) { + let mut rng = SmallRng::from_seed([42; 32]); + + let data: Vec = (0..1000).map(|_| rng.gen()).collect(); + + c.bench_function("std::sort_unstable_f32", |b| { + b.iter_batched( + || data.clone(), + |mut slice| black_box(slice.sort_unstable_by(|a, b| a.total_cmp(b))), + BatchSize::SmallInput, + ) + }); +} + +fn bench_sort_u8(c: &mut Criterion) { + let mut rng = SmallRng::from_seed([42; 32]); + + let data: Vec = (0..1000).map(|_| rng.gen()).collect(); + + c.bench_function("std::sort_unstable_u8", |b| { + b.iter_batched( + || data.clone(), + |mut slice| black_box(slice.sort_unstable()), + BatchSize::SmallInput, + ) + }); +} + +fn bench_sort_bool(c: &mut Criterion) { + let mut rng = SmallRng::from_seed([42; 32]); + + let data: Vec = (0..1000).map(|_| rng.gen()).collect(); + + c.bench_function("std::sort_unstable_bool", |b| { + b.iter_batched( + || data.clone(), + |mut slice| black_box(slice.sort_unstable()), + BatchSize::SmallInput, + ) + }); +} + +fn bench_sort_u128(c: &mut Criterion) { + let mut rng = SmallRng::from_seed([42; 32]); + + let data: Vec = (0..1000).map(|_| rng.gen()).collect(); + + c.bench_function("std::sort_unstable_u128", |b| { + b.iter_batched( + || data.clone(), + |mut slice| black_box(slice.sort_unstable()), + BatchSize::SmallInput, + ) + }); +} + +fn bench_sort_f64(c: &mut Criterion) { + let mut rng = SmallRng::from_seed([42; 32]); + + let data: Vec = (0..1000).map(|_| rng.gen()).collect(); + + c.bench_function("std::sort_unstable_f64", |b| { + b.iter_batched( + || data.clone(), + |mut slice| black_box(slice.sort_unstable_by(|a, b| a.total_cmp(b))), + BatchSize::SmallInput, + ) + }); +} + +criterion_group!( + benches, + bench_sort_i32, + bench_sort_f32, + bench_sort_f64, + bench_sort_u8, + bench_sort_bool, + bench_sort_u128 +); +criterion_main!(benches);