diff --git a/Cargo.toml b/Cargo.toml index b9cc8de..9d7ce23 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "big_o" -description = "Infers asymptotic computational complexity." -version = "0.1.0" +description = "Infers asymptotic computational complexity" +version = "0.1.1" edition = "2021" authors = ["Maksym Arutyunyan"] repository = "https://github.com/maksym-arutyunyan/big_o" diff --git a/README.md b/README.md index 6a226e2..1857968 100644 --- a/README.md +++ b/README.md @@ -4,16 +4,18 @@ Infers asymptotic computational complexity. `big_o` helps to estimate computational complexity of algorithms by inspecting measurement data (eg. execution time, memory consumption, etc). Users are expected to provide measurement data, `big_o` will try to fit a set of complexity models and return the best fit. -## Examples +## Example + +```rust +use assert_approx_eq::assert_approx_eq; -```rs // f(x) = gain * x ^ 2 + offset let data = vec![(1., 1.), (2., 4.), (3., 9.), (4., 16.)]; -let (best, _all) = big_o::infer_complexity(data).unwrap(); +let (complexity, _all) = big_o::infer_complexity(data).unwrap(); -assert_eq!(best.name, big_o::Name::Quadratic); -assert_eq!(best.notation, "O(n^2)"); -assert_approx_eq::assert_approx_eq!(best.params.gain.unwrap(), 1.0, 1e-6); -assert_approx_eq::assert_approx_eq!(best.params.offset.unwrap(), 0.0, 1e-6); +assert_eq!(complexity.name, big_o::Name::Quadratic); +assert_eq!(complexity.notation, "O(n^2)"); +assert_approx_eq!(complexity.params.gain.unwrap(), 1.0, 1e-6); +assert_approx_eq!(complexity.params.offset.unwrap(), 0.0, 1e-6); ``` diff --git a/src/lib.rs b/src/lib.rs index b12dac5..2c94a79 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,20 @@ //! `big_o` helps to estimate computational complexity of algorithms by inspecting measurement data //! (eg. execution time, memory consumption, etc). Users are expected to provide measurement data, //! `big_o` will try to fit a set of complexity models and return the best fit. +//! +//! # Example +//! ``` +//! # use assert_approx_eq::assert_approx_eq; +//! // f(x) = gain * x ^ 2 + offset +//! let data = vec![(1., 1.), (2., 4.), (3., 9.), (4., 16.)]; +//! +//! let (complexity, _all) = big_o::infer_complexity(data).unwrap(); +//! +//! assert_eq!(complexity.name, big_o::Name::Quadratic); +//! assert_eq!(complexity.notation, "O(n^2)"); +//! assert_approx_eq!(complexity.params.gain.unwrap(), 1.0, 1e-6); +//! assert_approx_eq!(complexity.params.offset.unwrap(), 0.0, 1e-6); +//! ``` mod complexity; mod linalg; @@ -18,28 +32,29 @@ pub use crate::params::Params; /// /// # Example /// ``` +/// # use assert_approx_eq::assert_approx_eq; /// // f(x) = gain * x ^ 2 + offset /// let data = vec![(1., 1.), (2., 4.), (3., 9.), (4., 16.)]; /// -/// let (best, _all) = big_o::infer_complexity(data).unwrap(); +/// let (complexity, _all) = big_o::infer_complexity(data).unwrap(); /// -/// assert_eq!(best.name, big_o::Name::Quadratic); -/// assert_eq!(best.notation, "O(n^2)"); -/// assert_approx_eq::assert_approx_eq!(best.params.gain.unwrap(), 1.0, 1e-6); -/// assert_approx_eq::assert_approx_eq!(best.params.offset.unwrap(), 0.0, 1e-6); +/// assert_eq!(complexity.name, big_o::Name::Quadratic); +/// assert_eq!(complexity.notation, "O(n^2)"); +/// assert_approx_eq!(complexity.params.gain.unwrap(), 1.0, 1e-6); +/// assert_approx_eq!(complexity.params.offset.unwrap(), 0.0, 1e-6); /// ``` pub fn infer_complexity( data: Vec<(f64, f64)>, ) -> Result<(Complexity, Vec), &'static str> { - let mut fitted: Vec = Vec::new(); + let mut all_fitted: Vec = Vec::new(); for name in name::all_names() { let complexity = complexity::fit(name, data.clone())?; if validate::is_valid(&complexity) { - fitted.push(complexity); + all_fitted.push(complexity); } } - fitted.sort_by(|a, b| a.params.residuals.partial_cmp(&b.params.residuals).unwrap()); - let best = fitted[0].clone(); + all_fitted.sort_by(|a, b| a.params.residuals.partial_cmp(&b.params.residuals).unwrap()); + let best_complexity = all_fitted[0].clone(); - Ok((best, fitted)) + Ok((best_complexity, all_fitted)) } diff --git a/tests/execution_time.rs b/tests/execution_time.rs index 3440895..aff1b61 100644 --- a/tests/execution_time.rs +++ b/tests/execution_time.rs @@ -1,11 +1,12 @@ use std::time::Instant; use std::{thread, time}; +use std::fmt::Write as _; // import without risk of name clashing #[allow(dead_code)] fn write_csv(data: &Vec<(f64, f64)>, path: &str) { let mut text = String::new(); for (x, y) in data { - text.push_str(&format!("{},{}\n", *x, *y)); + let _ = writeln!(text, "{},{}", *x, *y); } std::fs::write(path, text).expect("Unable to write file"); }