How can measure time in several programs

I'm thinking to measure time in different parts of several programs in crate (initialization struct, executing etc)
First: I need to pass this struct between programs, how?
Second: for example

//let mut threads = Vec::<chrono::Duration>::with_capacity(N);
           if pprint{
//will print last N  
                (0..N).for_each(|seq_num| { // <---- Closure 1
                //access last N in vec
                let arc_clone = Arc::clone(&self.0);
                let size_vec= arc_clone.lock().unwrap().len();
                    thread::spawn(move || {  // <---- Closure 2
                        let loc_clone = arc_clone.lock().unwrap();
                        let cur_loc= loc_clone.get(size_vec- seq_num as usize).unwrap();
                        let lt = &Local::now();
                        let next_loc= loc_clone.get(size_vec- seq_num +1 as usize).unwrap_or_else(|| lt);
                        let utc_time = DateTime::<Utc>::from_utc(next_loc.naive_utc(), Utc);
                        let dif_locs = next_loc.signed_duration_since(*cur_loc); // difference between locals
                        println!("Element {} was executed in {:?}\n",  size_vec - seq_num, dif_locs);

If i will uncomment threads- then will be
move occurs because threads has type std::vec::Vec<chrono::Duration>, which does not implement the Copy trait
I saw in somewhat crate this, but forget and can't find the same way now

 let lt = &Local::now();
 let next_loc= loc_clone.get(size_vec- seq_num +1 as usize).unwrap_or_else(|| lt);

I create variable lt on stack, but if pass

 let next_loc= loc_clone.get(size_vec- seq_num +1 as usize).unwrap_or_else(|| &Local::now(););

or Local::now();
will be problem as temporal value, but i don't create not temporary.
And if you know crates to do the same(maybe also interest in (microcontrollers) embedded rust), will be grateful.
Purpose: simply choose blocks and count time

There are several options:

  • Use an Arc<Mutex<Vec<Duration>>>, so each thread can get exclusive access to the Vec by locking it. You'd need to make sure each thread holds the lock for as short a time as possible, or it may prevent other threads from proceeding.

    Your existing code has this problem already, because it calls arc_clone.lock() and holds that lock for the entire lifetime of the thread. This means that only one thread will be able to do work at a time. If you don't need mutable access to your Arc, you should get rid of the lock and instead read directly from the Arc.

  • Instead of pushing the data to a vector in the child thread, return it from the thread::spawn closure. In the main thread, store all of the JoinHandles returned from thread::spawn, and call join on each of them to access the data they returned.

  • Use a crate like rayon that provides parallel iterators over vectors and other collections, so you can give each thread access to just one element of the vector.

1 Like

But as I asked also, don't you know how to pass the same struct between programs in the same crate?)

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.