Skip to content

Instantly share code, notes, and snippets.

@wayslog
Last active July 18, 2017 09:33
Show Gist options
  • Save wayslog/8524631923cd10f4aedbe62a153c6361 to your computer and use it in GitHub Desktop.
Save wayslog/8524631923cd10f4aedbe62a153c6361 to your computer and use it in GitHub Desktop.
#![feature(iterator_step_by)]
use std::time::SystemTime;
// 10! = 3_628_800
const TIMES: usize = 3_628_800 * 100;
fn lock_concurrency(worker: usize) {
use std::sync::{Mutex, Arc};
use std::thread;
let locker = Arc::new(Mutex::new(0u32));
let ths = (0..worker).into_iter().map(|_| {
let locker = locker.clone();
thread::spawn(move || {
for _ in 0..(TIMES / worker) {
let mut number = locker.lock().unwrap();
*number += 1;
}
})
});
for th in ths {
th.join().unwrap();
}
}
fn run_test() {
for x in (0..101).step_by(10) {
print!("test lock {} times in {} threads", TIMES, x);
let now = SystemTime::now();
lock_concurrency(x);
let es = now.elapsed().unwrap();
println!(" with {} seconds",
(es.as_secs() * 1_000_000_000 as u64 + es.subsec_nanos() as u64) as f64 /
1_000_000_000.0);
}
}
fn main() {
run_test();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment