Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/junegunn/lps
lps: rate-controlled loop execution
https://github.com/junegunn/lps
Last synced: 28 days ago
JSON representation
lps: rate-controlled loop execution
- Host: GitHub
- URL: https://github.com/junegunn/lps
- Owner: junegunn
- License: mit
- Created: 2012-04-23T09:13:04.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2013-09-06T08:29:17.000Z (about 11 years ago)
- Last Synced: 2024-08-11T09:50:59.242Z (3 months ago)
- Language: Ruby
- Size: 129 KB
- Stars: 3
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
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
-----```ruby
# - 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
------------------------```ruby
LPS.freq(10).loop { break if rand(10) == 0 }
```Changing loop frequency
-----------------------```ruby
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.```ruby
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
endputs [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
```