diff --git a/Cargo.lock b/Cargo.lock index 856fe207..f857cb4b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -248,6 +248,17 @@ version = "0.1.0" name = "geodesy" version = "0.1.0" +[[package]] +name = "getrandom" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe9006bed769170c11f845cf00c7c1e9092aeb3f268e007c3e760ac68008070f" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "graphs" version = "0.1.0" @@ -309,9 +320,9 @@ version = "0.1.0" [[package]] name = "libc" -version = "0.2.147" +version = "0.2.150" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" +checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c" [[package]] name = "linux-raw-sys" @@ -410,6 +421,12 @@ dependencies = [ "plotters-backend", ] +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + [[package]] name = "proc-macro2" version = "1.0.66" @@ -428,6 +445,36 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + [[package]] name = "rayon" version = "1.7.0" @@ -483,6 +530,7 @@ checksum = "e5ea92a5b6195c6ef2a0295ea818b312502c6fc94dde986c5553242e18fd4ce2" name = "robert" version = "0.1.0" dependencies = [ + "rand", "sorts", ] @@ -596,6 +644,12 @@ dependencies = [ "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.87" diff --git a/robert/Cargo.toml b/robert/Cargo.toml index 79d9728c..9423cc6c 100644 --- a/robert/Cargo.toml +++ b/robert/Cargo.toml @@ -5,4 +5,5 @@ edition = "2021" publish = false [dependencies] +rand = "0.8.5" sorts = { path = "../sorts" } diff --git a/robert/src/bin/sort_compare.rs b/robert/src/bin/sort_compare.rs new file mode 100644 index 00000000..88da4498 --- /dev/null +++ b/robert/src/bin/sort_compare.rs @@ -0,0 +1,54 @@ +// Copyright (c) 2020 Xu Shaohua . All rights reserved. +// Use of this source is governed by General Public License that can be found +// in the LICENSE file. + +use rand::Rng; +use std::time::Instant; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Variant { + InsertionSort, + SelectionSort, + ShellSort, +} + +fn time_sort(array: &mut [f64], variant: Variant) -> u128 { + let instance = Instant::now(); + match variant { + Variant::InsertionSort => sorts::insertion_sort(array), + Variant::SelectionSort => sorts::selection_sort(array), + Variant::ShellSort => sorts::shell_sort(array), + } + return instance.elapsed().as_nanos(); +} + +fn time_random_input(variant: Variant, len: usize, times: i32) -> u128 { + let mut total = 0; + let mut rng = rand::thread_rng(); + let mut array = vec![0.0; len]; + for _t in 0..times { + for i in 0..len { + array[i] = rng.gen(); + } + total += time_sort(&mut array, variant); + } + + return total; +} + +fn main() { + let args: Vec = std::env::args().collect(); + if args.len() != 3 { + panic!("Usage: number times"); + } + let num: usize = args[1].parse().expect("Invalid total length"); + let times: i32 = args[2].parse().expect("Invalid times"); + let t1 = time_random_input(Variant::InsertionSort, num, times); + let t2 = time_random_input(Variant::SelectionSort, num, times); + let t3 = time_random_input(Variant::ShellSort, num, times); + + println!("Insertion: {t1}, Selection: {t2}, ShellSort: {t3}"); + let ratio1: f64 = (t1 as f64) / (t3 as f64); + let ratio2: f64 = t2 as f64 / t3 as f64; + println!("Insertion ratio: {ratio1:.2}, Selection ratio: {ratio2:.2}, Shell ratio: 1.0"); +} diff --git a/robert/src/bin/sort_compare.rs.old b/robert/src/bin/sort_compare.rs.old deleted file mode 100644 index c5f437f9..00000000 --- a/robert/src/bin/sort_compare.rs.old +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright (c) 2020 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be found -// in the LICENSE file. - -use rand::Rng; -use std::time::Instant; - -fn time_algo(algorithm: &str, array: &mut Vec) -> u128 { - let instance = Instant::now(); - match algorithm { - "Insertion" => sort::insertion_sort(array), - "Selection" => sort::selection_sort(array), - "Shell" => sort::shell_sort(array), - _ => panic!("Invalid algorithm: {}", algorithm), - } - return instance.elapsed().as_nanos(); -} - -fn time_random_input(algorithm: &str, len: usize, times: i32) -> u128 { - let mut total = 0; - let mut rng = rand::thread_rng(); - let mut array = vec![0.0; len]; - for _t in 0..times { - for i in 0..len { - array[i] = rng.gen(); - } - total += time_algo(algorithm, &mut array); - } - - return total; -} - -fn main() { - let args: Vec = std::env::args().collect(); - if args.len() != 5 { - panic!("Usage: algo1 algo2 number times"); - } - let algo1 = &args[1]; - let algo2 = &args[2]; - let num: usize = args[3].parse().expect("Invalid total length"); - let times: i32 = args[4].parse().expect("Invalid times"); - let t1 = time_random_input(algo1, num, times); - let t2 = time_random_input(algo2, num, times); - - println!("t1: {}, t2: {}", t1, t2); - let times: f64 = t2 as f64 / t1 as f64; - println!("{} is {:.2} times faster than {}", algo1, times, algo2); -}