feat: parallelize pow gen

This commit is contained in:
Aravinth Manivannan 2023-07-09 16:15:16 +05:30
parent 4bc6450d22
commit c4bc8df0b8
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88

View file

@ -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();
});
}
}