Created
May 10, 2015 04:20
-
-
Save diegogub/51737e7e02ae56c135b1 to your computer and use it in GitHub Desktop.
rust : sync file
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
extern crate bincode; | |
extern crate time; | |
extern crate rustc_serialize; | |
use std::fs::OpenOptions; | |
use std::fs::File; | |
use std::io::prelude::*; | |
use bincode::SizeLimit; | |
use std::io::SeekFrom; | |
use std::mem; | |
#[derive(RustcEncodable, RustcDecodable, PartialEq)] | |
struct Entity { | |
x: f32, | |
y: f32, | |
} | |
#[derive(RustcEncodable, RustcDecodable, PartialEq)] | |
struct World { | |
entities: Vec<Entity> | |
} | |
fn main() { | |
let mut world = World { | |
entities: vec![Entity {x: 0.332, y: 4.0}, Entity {x: 10.0, y: 20.5},Entity {x: 1.0, y: 25.12},Entity {x: 1.1, y: 25.123}] | |
}; | |
let encoded: Vec<u8> = bincode::encode(&world, SizeLimit::Infinite).unwrap(); | |
println!("size:{}",encoded.len()); | |
let mut file = match OpenOptions::new().write(true).read(true).append(true).open("test.txt") { | |
Ok(file) => file, | |
Err(e) => panic!(e), | |
}; | |
println!("file opened"); | |
let now = time::get_time(); | |
for i in 0..1000{ | |
println!("line:{}",i); | |
let mut writen = match file.write(&encoded[..]) { | |
Ok(writen) => writen, | |
Err(e) => { | |
println!("failed to write: {}",e); | |
0 | |
} | |
}; | |
file.sync_data(); | |
} | |
let after = time::get_time() ; | |
println!("now:{}",now.nsec ); | |
println!("after:{}",after.nsec ); | |
println!("time:{}",(after.sec-now.sec ) * 1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment