extern crate time; use std::env; use std::ptr; use time::Duration; const SIZE: usize = 10 * 1024 * 1024; fn copy_iter(src: &[u8], dst: &mut Vec) { for &byte in src { dst.push(byte); } } fn copy_index(src: &[u8], dst: &mut Vec) { let mut index = 0; unsafe { dst.set_len(SIZE); } while index < src.len() { dst[index] = src[index]; index += 1; } } fn copy_push_all(src: &[u8], dst: &mut Vec) { dst.extend_from_slice(src); } fn copy_ptr(src: &[u8], dst: &mut Vec) { unsafe { dst.set_len(SIZE); ptr::copy_nonoverlapping(src.as_ptr(), (&mut dst[..]).as_mut_ptr(), SIZE); } } fn main() { let args: Vec<_> = env::args().collect(); if args.len() < 2 { panic!("Provide method") } let src = vec![1; SIZE]; let mut dst = Vec::with_capacity(SIZE); println!("{}", Duration::span(|| { match &(args[1])[..] { "iter" => copy_iter(&src[..], &mut dst), "index" => copy_index(&src[..], &mut dst), "push_all" => copy_push_all(&src[..], &mut dst), "ptr" => copy_ptr(&src[..], &mut dst), _ => println!("Wrong method"), } })); println!("{:?}", &dst[..10]); }