Created
March 20, 2021 23:29
-
-
Save lpj145/0f903bfdbac6562a0635089b0f9ef3ea to your computer and use it in GitHub Desktop.
Processar 9 milhoes de linhas csv com rust.
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::{collections::HashMap, fs::File, io::{self, BufRead, BufReader}}; | |
use std::time::{SystemTime, UNIX_EPOCH}; | |
use core::time::Duration; | |
pub fn get_unix_timestamp_ms() -> Duration { | |
SystemTime::now() | |
.duration_since(UNIX_EPOCH) | |
.expect("Time went problem.") | |
} | |
pub fn microtime() -> u64 { | |
let start_time = get_unix_timestamp_ms(); | |
start_time.as_secs() * 1000 + start_time.subsec_nanos() as u64 / 1_000_000 | |
} | |
fn main() -> io::Result<()>{ | |
let start_ms = microtime(); | |
let filepath = "./a.csv"; | |
let file = File::open(filepath).expect("What is ./a.csv file ?"); | |
let reader = BufReader::new(file); | |
let mut consumer_reviews: HashMap<String, u32> = HashMap::new(); | |
for line in reader.lines() { | |
let copy = line?; | |
let indexes = copy.split(','); | |
let mut current_client = String::from(""); | |
for (index, value) in indexes.enumerate() { | |
let _value = String::from(value); | |
// Client index | |
if index == 0 { | |
current_client = _value.clone(); | |
consumer_reviews.entry(_value).or_insert(0); | |
continue; | |
} | |
// Food index | |
if index == 1 { | |
*consumer_reviews.entry(_value).or_insert(0) += 1; | |
continue; | |
} | |
if index == 2 { | |
let price: u32 = _value.parse().unwrap_or(0); | |
*consumer_reviews.entry(current_client.clone()).or_insert(0) += price; | |
} | |
} | |
} | |
println!("Result: {:?}", consumer_reviews); | |
println!("File path: {:?}", filepath); | |
println!("End in: {} ms or {} seconds.", (microtime() - start_ms), (microtime() - start_ms) / 1000); | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Versão que clona os &str somente quando precisa inserir uma nova entrada na HashMap: