Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::{fs::OpenOptions, fs::File, time::Instant, path::Path};
- use memmap2::Mmap;
- use memmap2::MmapMut;
- pub fn spease_one(input: impl AsRef<Path>, output: impl AsRef<Path>) -> std::io::Result<()> {
- let (chars_to_write, total_len) = {
- let f = File::open(input)?;
- let fm = unsafe { Mmap::map(&f) }?;
- #[cfg(unix)]
- fm.advise(Advice::Sequential)?;
- let mut char_counter = [0u64; 256];
- for c in fm.as_ref() {
- *unsafe { char_counter.get_unchecked_mut(*c as usize) } += 1;
- }
- (char_counter, fm.len())
- };
- let of = OpenOptions::new().read(true).write(true).create(true).open(output)?;
- of.set_len(total_len as u64)?;
- let mut ofm = unsafe { MmapMut::map_mut(&of) }?;
- let mut writer = ofm.as_mut();
- for (c,v) in chars_to_write.into_iter().enumerate().map(|(c,v)| (c as u8, v as usize)) {
- if c != b' ' {
- let (begin, end) = writer.split_at_mut(v);
- begin.fill(c);
- writer = end;
- }
- }
- writer.fill(b' ');
- Ok(())
- }
- fn main() {
- let start = Instant::now();
- spease_one("input.txt", "output").unwrap();
- println!("Rust Time {:?}", start.elapsed());
- }
Advertisement
Add Comment
Please, Sign In to add comment