Advertisement
Guest User

Untitled

a guest
May 26th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.06 KB | None | 0 0
  1. #![feature(box_syntax)]
  2.  
  3. use crossbeam_utils::thread;
  4. use std::time::Instant;
  5.  
  6. const MAP_WIDTH: usize = 8192;
  7. const MAP_ARRAY_SIZE: usize = MAP_WIDTH * MAP_WIDTH;
  8. const NUM_CORES: usize = 4;
  9.  
  10. fn main() {
  11.     let t1_a = Instant::now();
  12.  
  13.     let mut a: Box<[u64; MAP_ARRAY_SIZE]> = box [0x00; MAP_ARRAY_SIZE];
  14.  
  15.     assert_eq!(MAP_ARRAY_SIZE % NUM_CORES, 0);
  16.  
  17.     thread::scope(|scope_handle| {
  18.         for chunk in a.chunks_mut(MAP_ARRAY_SIZE / 4) {
  19.             scope_handle.spawn(move |_| {
  20.                 do_stuff(chunk, 0x01);
  21.             });
  22.         }
  23.     }).unwrap();
  24.  
  25.     assert_eq!((*a)[0], 1);
  26.  
  27.     let t2_a = Instant::now();
  28.     println!("chunks {:?}", t2_a - t1_a);
  29.  
  30.     /////////////////////////////////////
  31.  
  32.     let t1_b = Instant::now();
  33.     let mut b: Box<[u64; MAP_ARRAY_SIZE]> = box [0x00; MAP_ARRAY_SIZE];
  34.     do_stuff(&mut (*b), 0x01);
  35.     assert_eq!((*b)[0], 1);
  36.     let t2_b = Instant::now();
  37.     println!("linear {:?}", t2_b - t1_b);
  38. }
  39.  
  40. fn do_stuff(a: &mut [u64], n: u64) {
  41.     for x in a.iter_mut() {
  42.         *x = n;
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement