Skip to content

Instantly share code, notes, and snippets.

@ashish01
Last active February 25, 2016 21:40
Show Gist options
  • Save ashish01/e0e489e4849412e21a57 to your computer and use it in GitHub Desktop.
Save ashish01/e0e489e4849412e21a57 to your computer and use it in GitHub Desktop.
#![feature(time2)]
use std::time::Instant;
use std::thread;
use std::sync::mpsc;
use std::io;
struct ScopedPerformanceBlock<'a> {
sender: &'a mpsc::Sender<u32>,
creation_time: Instant,
}
impl<'a> Drop for ScopedPerformanceBlock<'a> {
fn drop(&mut self) {
let diff = self.creation_time.elapsed().subsec_nanos();
self.sender.send(diff).unwrap();
}
}
impl<'a> ScopedPerformanceBlock<'a> {
fn new(sender: &mpsc::Sender<u32>) -> ScopedPerformanceBlock {
ScopedPerformanceBlock {
creation_time: Instant::now(),
sender: sender
}
}
}
fn main() {
let (tx, rx) = mpsc::channel::<u32>();
thread::spawn(move || {
loop {
println!("{:?}", rx.recv().unwrap());
}
});
for i in 1..100
{
let x = ScopedPerformanceBlock::new(&tx);
}
let mut input = String::new();
io::stdin().read_line(&mut input);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment