Giter Site home page Giter Site logo

nickataccups / spin-sleep Goto Github PK

View Code? Open in Web Editor NEW

This project forked from alexheretic/spin-sleep

0.0 0.0 0.0 52 KB

Rust accurate sleeping. Only use native sleep as far as it can be trusted, then spin.

License: Apache License 2.0

Shell 2.29% Rust 97.71%

spin-sleep's Introduction

spin_sleep crates.io Documentation

Accurate sleeping. Only use native sleep as far as it can be trusted, then spin.

The problem with thread::sleep is it isn't always very accurate, and this accuracy varies on platform and state. Spinning is as accurate as we can get, but consumes the CPU rather ungracefully.

This library adds a middle ground, using a configurable native accuracy setting allowing thread::sleep to wait the bulk of a sleep time, and spin the final section to guarantee accuracy.

SpinSleeper

Simplist usage with default native accuracy is a drop in replacement for thread::sleep.

spin_sleep::sleep(Duration::new(1, 12_550_000));

More advanced usage, including setting a custom native accuracy, can be achieved by constructing a SpinSleeper.

// Create a new sleeper that trusts native thread::sleep with 100μs accuracy
let spin_sleeper = spin_sleep::SpinSleeper::new(100_000);

// Sleep for 1.01255 seconds, this will:
//  - thread:sleep for 1.01245 seconds, ie 100μs less than the requested duration
//  - spin until total 1.01255 seconds have elapsed
spin_sleeper.sleep(Duration::new(1, 12_550_000));

Sleep can also requested in f64 seconds or u64 nanoseconds (useful when used with time crate)

spin_sleeper.sleep_s(1.01255);
spin_sleeper.sleep_ns(1_012_550_000);

OS-specific default accuracy settings should be good enough for most cases.

let sleeper = SpinSleeper::default();

LoopHelper

For controlling & report rates (e.g. game FPS) this crate provides LoopHelper. A SpinSleeper is used to maximise sleeping accuracy.

use spin_sleep::LoopHelper;

let mut loop_helper = LoopHelper::builder()
    .report_interval_s(0.5) // report every half a second
    .build_with_target_rate(250.0); // limit to 250 FPS if possible

let mut current_fps = None;

loop {
    let delta = loop_helper.loop_start(); // or .loop_start_s() for f64 seconds

    // compute_something(delta);

    if let Some(fps) = loop_helper.report_rate() {
        current_fps = Some(fps);
    }

    // render_fps(current_fps);

    loop_helper.loop_sleep(); // sleeps to acheive a 250 FPS rate
}

Windows Accuracy

Windows has particularly poor accuracy by default (~15ms), spin_sleep will automatically select the best accuracy on windows generally achieving ~1ms native sleep accuracy (Since 0.3.3).

Minimum supported rust compiler

This crate is maintained with latest stable rust.

spin-sleep's People

Contributors

alexheretic 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.