sorting/
main.rs

1//! # Sorting algorithms
2//!
3//! This example shows how to use the library to measure the time complexity of sorting algorithms.
4
5use chrono_probe::{
6    input::{distribution::Exponential, InputBuilder},
7    measurements::measure_mut,
8    plot::time_plot,
9};
10use chrono_probe::plot::PlotConfig;
11
12use crate::algorithms::{merge_sort_input, quick_sort_input};
13
14mod algorithms;
15mod input;
16
17fn main() {
18    // Create a distribution for the length of the vectors
19    // Here we use an exponential distribution with a minimum of 1000 and a maximum of 500_000
20    let length_distribution = Exponential::new(1000..=500_000);
21
22    // Create the builder for the vectors
23    let vector_builder = InputBuilder::new(length_distribution, ());
24
25    // Build the vectors
26    // Here we build 2000 vectors, 10 of each length
27    let mut vectors = vector_builder.build_with_repetitions(200, 10);
28
29    // Create a slice of the algorithms we want to measure
30    let algorithms: &[(fn(&mut input::InputVec), &str); 2] = &[
31        (merge_sort_input, "Merge sort"),
32        (quick_sort_input, "Quick sort"),
33    ];
34
35    // Measure the algorithms on the vectors, given a relative error of 0.001
36    let results = measure_mut(&mut vectors, algorithms, 0.001);
37
38    let result_clone = results.clone();
39    // Serialize the results to a json file
40    result_clone.serialize_json("results.json");
41
42    let file_name = "results/sorting.svg";
43
44    // Plot the results
45    let config = PlotConfig::default()
46        .with_title("Sorting algorithms")
47        .with_caption("The time plot of sorting algorithms");
48
49    time_plot(file_name, results, &config);
50}