Crates.io | time_wheel |
lib.rs | time_wheel |
version | 0.1.2 |
source | src |
created_at | 2021-08-09 05:16:37.441845 |
updated_at | 2021-08-11 07:10:03.100543 |
description | An implementation of timer uses time-wheel algorithm |
homepage | |
repository | https://github.com/howz97/time_wheel |
max_upload_size | |
id | 433500 |
size | 24,341 |
An implementation of timer uses time-wheel algorithm
1
use std::time::{Duration, SystemTime};
use time_wheel::FrontEnd;
fn main() {
let mut fe = FrontEnd::new(Duration::from_millis(32), 60, 4);
let time0 = SystemTime::now();
let timer_id = fe.put_timer(Duration::from_millis(1500));
println!("timer_id = {}", timer_id);
let timer = fe.receiver.recv().unwrap();
println!("Trigger {:?}", timer);
println!("cost {:?}", time0.elapsed().unwrap());
}
output
timer_id = 1
Trigger Timer(1, 1628603696.593087s)
cost 1.507511s
2
use std::time::{Duration};
use time_wheel::{FrontEnd, unix_now_ms};
use std::thread::sleep;
fn main() {
let mut fe = FrontEnd::new(Duration::from_millis(32), 60, 4);
fe.after_func(Duration::from_secs(1), |t| {
println!(
" error={:?} function is executing", unix_now_ms() - t.when);
});
sleep(Duration::from_secs(3));
}
output
error=27.973ms function is executing
3
use std::time::{Duration};
use time_wheel::{FrontEnd, unix_now_ms};
fn main() {
let mut fe = FrontEnd::new(Duration::from_millis(32), 60, 4);
let ticker = fe.ticker(Duration::from_millis(100));
for i in 0..5 {
let when = ticker.recv().unwrap();
println!("on tick {} error={:?}", i, unix_now_ms() - when);
}
}
output
on tick 0 error=32.028ms
on tick 1 error=24.275ms
on tick 2 error=23.229ms
on tick 3 error=20.323ms
on tick 4 error=16.779ms