From c4bc8df0b8f9c06b8ab76c057b163ed81e599ecd Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Sun, 9 Jul 2023 16:15:16 +0530 Subject: [PATCH] feat: parallelize pow gen --- src/main.rs | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/src/main.rs b/src/main.rs index f392543..477d25d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ use clap::Parser; use pow_sha256::ConfigBuilder; +use rayon::prelude::*; use serde::{Deserialize, Serialize}; use std::time::Instant; @@ -36,28 +37,29 @@ impl GenerateDeriveArgs { let string = get_random(32); let pow_config = ConfigBuilder::default().salt(salt.clone()).build().unwrap(); - for difficulty in self.start..self.max { - let start = Instant::now(); - pow_config.prove_work(&string, difficulty).unwrap(); - let finish = Instant::now(); - let time_elapsed = finish.duration_since(start); + (self.start..self.max) + .into_par_iter() + .for_each(|difficulty| { + let start = Instant::now(); + pow_config.prove_work(&string, difficulty).unwrap(); + let finish = Instant::now(); + let time_elapsed = finish.duration_since(start); - let time= time_elapsed.as_micros(); - log::info!("Difficulty factor {difficulty} generated in {time}"); + let time = time_elapsed.as_micros(); + log::info!("Difficulty factor {difficulty} generated in {time}"); + let log = Log { + salt: salt.clone(), + time, + string: string.clone(), + }; - let log = Log { - salt: salt.clone(), - time, - string: string.clone(), - }; - - db.insert( - bincode::serialize(&difficulty).unwrap(), - bincode::serialize(&log).unwrap(), - ) - .unwrap(); - } + db.insert( + bincode::serialize(&difficulty).unwrap(), + bincode::serialize(&log).unwrap(), + ) + .unwrap(); + }); } }