Hello, I have been doing rust for quite a while and i do not have cpp background. I am using the below code for taking input from terminal.
What is the best method to take such large input multiple time, also i know only little cpp and do not know how cpp program is taking less time.
Input format:
10566000
100000 1
100000 1
100000 1
100000 1
100000 1
100000 1
100000 1
100000 1
. .
. .
. . 10566000 times
// cpp
#include<bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int case;
cin>>case;
int c = 0;
while(case > 0)
{
int x,y;
cin>>x>>y;
c += 1;
case -= 1;
}
cout<<c<<endl;
}
//$ time bash v_cpp.sh
// 10566000
// real 0m1.027s
// user 0m0.993s
// sys 0m0.083s
original post by @kkroy22
// @kkroy22
use std::io::{stdin, BufRead, BufReader};
fn main() {
let mut buf = BufReader::new(stdin()).lines();
let mut case = buf.next().unwrap().unwrap().parse::<usize>().unwrap();
let mut c = 0;
while case > 0 {
let xy = buf.next().unwrap().unwrap().split(" ").map(|x| x.parse::<usize>().unwrap()).collect::<Vec<usize>>();
let _x = xy[0];
let _y = xy[1];
c += 1;
case -= 1;
}
println!("{}", c);
}
//$ time bash v_rust.sh
// 10566000
// real 0m1.801s
// user 0m1.790s
// sys 0m0.094s
Optimization by @notoria
// @notoria
use std::io::{stdin, BufRead, BufReader};
fn main() {
let s = stdin();
let mut buf = BufReader::new(s.lock()).lines();
let mut case = buf.next().unwrap().unwrap().parse::<usize>().unwrap();
let mut c = 0;
while case > 0 {
let input = buf.next().unwrap().unwrap();
let mut iter = input.split(' ');
let _x: usize = iter.next().map(|x| x.parse::<usize>().unwrap()).unwrap();
let _y: usize = iter.next().map(|x| x.parse::<usize>().unwrap()).unwrap();
c += 1;
case -= 1;
}
println!("{}", c);
}
//$ time bash v_rust.sh
// 10566000
// real 0m0.777s
// user 0m0.764s
// sys 0m0.060s
Cpp equivalence by by @Michael-F-Bryan
// @Michael-F-Bryan
use std::io::{stdin, BufRead, BufReader};
fn main() {
let s = stdin();
let mut reader = BufReader::new(s.lock());
let mut line = String::new();
reader.read_line(&mut line);
let mut case: usize = line.trim().parse().unwrap();
let mut c = 0;
while case > 0 {
line.clear();
reader.read_line(&mut line);
let mut words = line.trim().split(" ");
let _x: usize = words.next().unwrap().parse().unwrap();
let _y: usize = words.next().unwrap().parse().unwrap();
c += 1;
case -= 1;
}
println!("{}", c);
}
//$ time bash v_rust.sh
// 10566000
// real 0m0.880s
// user 0m0.852s
// sys 0m0.080s
Thank you community !!