Testing speed improvements with threads



examples/threads/threads-load-test/src/main.rs
use std::time::Instant;
use std::sync::mpsc;
use std::thread;

fn main() {
    let (tx, rx) = mpsc::channel();

    let start = Instant::now();
    //for n in 1..=10 {
    //    println!("{n}: {}", fibonacci(n));
    //}

    //let n = 43; // 42 2 sec,   43 3 sec
    //println!("{n}: {}", fibonacci(n));

    //for _x in 1..=10 {
    //    let n = 42;
    //    println!("{n}: {}", fibonacci(n));
    //}

    for _x in 1..=10 {
        let txr = tx.clone();
        let n = 42;
        thread::spawn(move || {
            let res = fibonacci(n);
            txr.send(res.to_string()).unwrap();
            println!("spawned thread finished");
        });
    }
    drop(tx);

    for received in rx {
        println!("Got: {}", received);
    }

    let duration = start.elapsed();

    println!("Time elapsed in expensive_function() is: {:?}", duration);

}

fn fibonacci(n :u64) -> u64 {
    if n == 0 || n == 1 {
        return 1;
    }
    fibonacci(n - 1) + fibonacci(n - 2)
}

TODO: what if there are variables in the main function? Can we read them from the threads? Can we write them? TODO: How to share workload? e.g. We would like to create 10,000 files with the sequnce number of the file being botht the content and the filename. TODO: What if we have a vector of 10,000 values and we would like to save each one of them in a separate file?