Created
August 4, 2022 03:08
-
-
Save sumeet/c728f85aa73e4032071ab1ef4f11c12b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::fs::File; | |
use std::io::{BufRead, BufReader}; | |
use rayon::iter::{ParallelBridge}; | |
use serde::Deserialize; | |
use rayon::iter::ParallelIterator; | |
#[derive(Deserialize, Debug)] | |
struct Business { | |
stars: f64, | |
} | |
fn main() -> Result<(), Box<dyn std::error::Error>> { | |
let f = File::open("/home/sumeet/Downloads/yelp_dataset/yelp_academic_dataset_business.json")?; | |
let bufreader = BufReader::new(f); | |
let (len, sum) = bufreader.lines().par_bridge().map(|line| { | |
let b : Business = serde_json::from_str(line.as_ref().unwrap()).unwrap(); | |
(1, b.stars) | |
}).reduce(||(0, 0.), |(acc_ct, acc_rating), (ct, rating)| { | |
(acc_ct + ct, acc_rating + rating) | |
}); | |
dbg!(sum / len as f64); | |
Ok(()) | |
} | |
// > time target/debug/yelp | |
// [src/main.rs:21] sum / len as f64 = 3.5967235576603303 | |
// | |
// ________________________________________________________ | |
// Executed in 879.57 millis fish external | |
// usr time 855.58 millis 642.00 micros 854.93 millis | |
// sys time 23.54 millis 255.00 micros 23.28 millis | |
// > time target/release/yelp | |
// [src/main.rs:21] sum / len as f64 = 3.5967235576603303 | |
// | |
// ________________________________________________________ | |
// Executed in 140.42 millis fish external | |
// usr time 120.25 millis 701.00 micros 119.55 millis | |
// sys time 20.15 millis 223.00 micros 19.93 millis | |
// fn old_main() { | |
// let f = File::open("/home/sumeet/Downloads/yelp_dataset/yelp_academic_dataset_business.json")?; | |
// let bufreader = BufReader::new(f); | |
// let mut len = 0; | |
// let mut sum = 0.; | |
// for line in bufreader.lines() { | |
// let line = line?; | |
// let b: Business = serde_json::from_str(&line)?; | |
// len += 1; | |
// sum += b.stars; | |
// } | |
// dbg!(sum / len as f64); | |
// | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment