Skip to content

Commit

Permalink
Use rayon
Browse files Browse the repository at this point in the history
  • Loading branch information
simonhammes committed May 26, 2024
1 parent e1fd71d commit 3e4ce67
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 12 deletions.
52 changes: 52 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ edition = "2021"
[dependencies]
chrono = "0.4.23"
futures = "0.3.26"
rayon = "1.6.1"
tokio = { version = "1.25.0", features = ["full"]}
19 changes: 7 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use chrono::{DateTime, Datelike, Days, NaiveDate, NaiveTime, Weekday};
use futures::future::join_all;
use rayon::prelude::*;
use std::collections::BTreeMap;
use std::thread;
use std::time::Duration;

#[tokio::main]
async fn main() {
fn main() {
println!("Hello, world!");

let start = NaiveDate::from_isoywd_opt(2023, 1, Weekday::Mon)
Expand All @@ -24,13 +24,8 @@ async fn main() {
.map(DateTime::date_naive)
.collect();

let futures = dates.iter().map(fetch);

let result: Vec<(&NaiveDate, Vec<Response>)> = join_all(futures)
.await
.into_iter()
.filter_map(Result::ok)
.collect();
let result: Vec<(&NaiveDate, Vec<Response>)> =
dates.par_iter().map(fetch).filter_map(Result::ok).collect();

let map = BTreeMap::from_iter(result);

Expand All @@ -43,9 +38,9 @@ struct Response {
day: u8,
}

async fn fetch(date: &NaiveDate) -> Result<(&NaiveDate, Vec<Response>), ()> {
fn fetch(date: &NaiveDate) -> Result<(&NaiveDate, Vec<Response>), ()> {
println!("Sleeping...");
tokio::time::sleep(Duration::from_secs(1)).await;
thread::sleep(Duration::from_secs(1));

let responses = vec![
Response {
Expand Down

0 comments on commit 3e4ce67

Please sign in to comment.