-
-
Save matklad/81b68284e0e6ba70c4c2 to your computer and use it in GitHub Desktop.
This file contains 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
-> % cat src/main.rs | |
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<u8>) { | |
// dst.extend(src.iter()) | |
for &byte in src { | |
dst.push(byte); | |
} | |
} | |
fn copy_index(src: &[u8], dst: &mut Vec<u8>) { | |
let mut index = 0; | |
unsafe { dst.set_len(SIZE); } | |
while index < src.len() { | |
dst[index] = src[index]; | |
index += 1; | |
} | |
} | |
// I'm using stable Rust, so I don't have extend_from_slice | |
// fn copy_push_all(src: &[u8], dst: &mut Vec<u8>) { | |
// dst.extend_from_slice(src); | |
// } | |
fn copy_ptr(src: &[u8], dst: &mut Vec<u8>) { | |
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]); | |
} | |
-> % cargo build --release | |
Compiling bench v0.1.0 (file:///home/matklad/trash/bench) | |
-> % cargo run --release -- iter | |
Running `target/release/bench iter` | |
PT0.030518169S | |
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1] | |
-> % cargo run --release -- iter | |
Running `target/release/bench iter` | |
PT0.029139180S | |
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1] | |
-> % cargo run --release -- iter | |
Running `target/release/bench iter` | |
PT0.029092613S | |
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1] | |
-> % cargo run --release -- index | |
Running `target/release/bench index` | |
PT0.004216764S | |
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1] | |
-> % cargo run --release -- index | |
Running `target/release/bench index` | |
PT0.004246780S | |
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1] | |
-> % cargo run --release -- index | |
Running `target/release/bench index` | |
PT0.004236901S | |
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1] | |
-> % cat src/main.rs | |
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<u8>) { | |
dst.extend(src.iter()) | |
// for &byte in src { | |
// dst.push(byte); | |
// } | |
} | |
fn copy_index(src: &[u8], dst: &mut Vec<u8>) { | |
let mut index = 0; | |
unsafe { dst.set_len(SIZE); } | |
while index < src.len() { | |
dst[index] = src[index]; | |
index += 1; | |
} | |
} | |
// I'm using stable Rust, so I don't have extend_from_slice | |
// fn copy_push_all(src: &[u8], dst: &mut Vec<u8>) { | |
// dst.extend_from_slice(src); | |
// } | |
fn copy_ptr(src: &[u8], dst: &mut Vec<u8>) { | |
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]); | |
} | |
-> % cargo build --release | |
Compiling bench v0.1.0 (file:///home/matklad/trash/bench) | |
-> % cargo run --release -- iter | |
Running `target/release/bench iter` | |
PT0.023198766S | |
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1] | |
-> % cargo run --release -- iter | |
Running `target/release/bench iter` | |
PT0.023328732S | |
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1] | |
-> % cargo run --release -- iter | |
Running `target/release/bench iter` | |
PT0.023282794S | |
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1] | |
-> % cargo run --release -- index | |
Running `target/release/bench index` | |
PT0.013653107S | |
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1] | |
-> % cargo run --release -- index | |
Running `target/release/bench index` | |
PT0.013599356S | |
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1] | |
-> % cargo run --release -- index | |
Running `target/release/bench index` | |
PT0.013615311S | |
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment