Skip to content

Instantly share code, notes, and snippets.

@dplord
Created April 23, 2015 07:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dplord/fa02a1cb27f779d70deb to your computer and use it in GitHub Desktop.
Save dplord/fa02a1cb27f779d70deb to your computer and use it in GitHub Desktop.
tenMillionFile.rs
use std::rand;
use std::rand::Rng;
use std::old_io::BufferedReader;
use std::old_io::File;
fn write_file(path: &str, s: &str)
{
use std::old_io::{BufferedWriter, File};
let file = File::create(&Path::new(path)).unwrap();
let mut writer = BufferedWriter::new(file);
writer.write_str(s).unwrap();
writer.flush().unwrap();
}
fn my_rand() -> i32 {
let mut rng = rand::thread_rng();
let n: i32 = rng.gen_range(1, 10000);
n
}
fn main()
{
for i in 0..10000
{
let mut s = String::with_capacity(262144);
for j in 0..10000
{
let s_one:String = format!("{}\t{}\t{}\t{}\t{}",
my_rand(),
my_rand(),
my_rand(),
my_rand(),
my_rand()
);
s.push_str(&s_one);
if(j != 9999) { s.push_str("\n"); }
}//生成10000行随机数
//println!("{}\n", i);
let path: String = format!("/tmp/out/r_{}", i);
write_file(&path, &s);
}// 生成10000个文件
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment