Last active
          March 30, 2018 12:40 
        
      - 
      
- 
        Save buckle2000/94a1284c73a3b7e3fbd04138e17054c1 to your computer and use it in GitHub Desktop. 
    Create fixed sized vector
  
        
  
    
      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
    
  
  
    
  | #![feature(test)] | |
| extern crate test; | |
| use test::Bencher; | |
| fn first(buffer_size: usize) -> Vec<u8> { | |
| vec![0; buffer_size] | |
| } | |
| fn second(buffer_size: usize) -> Vec<u8> { | |
| std::iter::repeat(0u8).take(buffer_size).collect() | |
| } | |
| #[bench] | |
| fn first_bench(b: &mut Bencher) { | |
| b.iter(|| first(100)); | |
| } | |
| #[bench] | |
| fn second_bench(b: &mut Bencher) { | |
| b.iter(|| second(100)); | |
| } | 
  
    
      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
    
  
  
    
  | $ cargo bench | |
| running 2 tests | |
| test first_bench ... bench: 22 ns/iter (+/- 0) | |
| test second_bench ... bench: 34 ns/iter (+/- 1) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment