Created
January 17, 2017 16:12
-
-
Save icefoxen/26691489a7d501e8ae6e266c1e084450 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
extern crate time; | |
// Single array of 10 000 000 will cause stack overflow, so outer and inner | |
const size_outer:usize = 1000; | |
const size_inner:usize = 10 * 1000; | |
fn create_vec() -> Vec<[usize;size_inner]> { | |
let mut data:Vec<[usize;size_inner]> = Vec::with_capacity(size_outer); | |
for _ in 0..size_outer { | |
data.push([0usize;size_inner]); | |
} | |
data | |
} | |
fn without_as_ref(data: Vec<[usize;size_inner]>) { | |
let t0 = time::precise_time_ns(); | |
{ | |
let result : Result<_,usize> = Ok(data); | |
match result { | |
Ok(data1) => {}, | |
Err(_) => {} | |
} | |
} | |
let t1 = time::precise_time_ns(); | |
println!("without_as_ref duration={}", (t1 - t0)); | |
} | |
fn with_as_ref(data: Vec<[usize;size_inner]>) { | |
let t0 = time::precise_time_ns(); | |
{ | |
let result : Result<_,usize> = Ok(data); | |
match result.as_ref() { | |
Ok(data1) => {}, | |
Err(_) => {} | |
} | |
} | |
let t1 = time::precise_time_ns(); | |
println!("with_as_ref duration={}", (t1 - t0)); | |
} | |
pub fn main() { | |
let v1 = create_vec(); | |
let v2 = create_vec(); | |
with_as_ref(v1); | |
without_as_ref(v2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results:
with_as_ref duration=7977913
without_as_ref duration=8698716