search/
main.rs

1//! # Search in an ordered vector
2//!
3//! This example shows how to use the library to measure the time complexity of
4//! searching algorithms in an ordered vector.
5
6use chrono_probe::{
7    input::{distribution::Uniform, InputBuilder},
8    measurements::measure,
9    plot::time_plot,
10};
11use chrono_probe::plot::PlotConfig;
12
13use crate::algorithms::{binary_search_input, linear_search_input};
14use crate::input::Generator;
15
16mod algorithms;
17mod input;
18
19fn main() {
20    // Create a distribution for the length of the vectors
21    // Here we use an uniform distribution with a minimum of 10 and a maximum of 100_000
22    let length_distribution = Uniform::new(10..=100_000);
23
24    // Create the builder for the vectors
25    // Here we choose to use the fast generator method in order to generate ordered vectors
26    let vector_builder = InputBuilder::new(length_distribution, Generator::Fast);
27
28    // Build 200 vectors
29    let vectors = vector_builder.build(200);
30
31    // Create a slice of the algorithms we want to measure
32    let algorithms: &[(fn(&input::SearchInput) -> Option<usize>, &str); 2] = &[
33        (linear_search_input, "Linear search"),
34        (binary_search_input, "Binary search"),
35    ];
36
37    // Measure the algorithms on the vectors, given a relative error of 0.001
38    let results = measure(&vectors, algorithms, 0.001);
39
40    let file_name = "results/search.svg";
41
42    // Here we print the linear regression of the log-log scale of the results
43    for result in results.clone().measurements {
44        let log_linear_regression = result.log_log_scale().linear_regression();
45        println!(
46            "{}: {} * x + {}",
47            result.algorithm_name, log_linear_regression.0, log_linear_regression.1
48        )
49    }
50
51    let config = PlotConfig::default()
52        .with_title("Search in an ordered vector")
53        .with_caption("The time plot of searching algorithms in an ordered vector");
54
55    // Plot the results
56    time_plot(file_name, results, &config);
57}