Skip to content

Commit

Permalink
Update docs (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
maksym-arutyunyan authored Jul 2, 2022
1 parent 533f705 commit 075267c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 20 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
```
35 changes: 25 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Complexity>), &'static str> {
let mut fitted: Vec<Complexity> = Vec::new();
let mut all_fitted: Vec<Complexity> = 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))
}
3 changes: 2 additions & 1 deletion tests/execution_time.rs
Original file line number Diff line number Diff line change
@@ -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");
}
Expand Down

0 comments on commit 075267c

Please sign in to comment.