Coder Social home page Coder Social logo

time_wheel's Introduction

timer

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

time_wheel's People

Contributors

howz97 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.