Giter VIP home page Giter VIP logo

lps's Introduction

LPS: Loops Per Second

Rate-controlled loop execution.

Installation

Add this line to your application's Gemfile:

gem 'lps'

And then execute:

$ bundle

Or install it yourself as:

$ gem install lps

Usage

# - Loops 10 times per second
# - Loops for 10 seconds
now = Time.now
LPS.freq(10).while { Time.now - now < 10 }.loop do
  # do something
end


# - Loops 10 times per second
# - Loops indefinitely
LPS.freq(10).loop do
  # do something
end


# Every 0.1 second
LPS.interval(0.1).loop do
  # do something
end

Breaking out of the loop

LPS.freq(10).loop { break if rand(10) == 0 }

Changing loop frequency

i = 0
LPS.while { i < 100 }.loop do |lps|
  print '.'
  lps.freq = i += 1
end

Falling behind

With LPS, the given loop block is run synchronously, which means that if the block execution takes longer than the interval for the given frequency, (e.g. 0.01 second for 100) it may not be possible to achieve the desired frequency.

12.times.map { |i| 1 << i }.each do |ps|
  cnt = 0
  now = Time.now
  LPS.freq(ps).while { Time.now - now <= 1 }.loop do
    cnt += 1
    sleep 0.01
  end

  puts [ps, cnt].join ' => '
end
1 => 1
2 => 2
4 => 4
8 => 8
16 => 16
32 => 32
64 => 64
128 => 98
256 => 98
512 => 99
1024 => 97
2048 => 98

lps's People

Contributors

junegunn avatar

Watchers

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