# Time based loop limiting

**URL:** <https://rubytalk.org/t/time-based-loop-limiting/55298>\
**Category:** ruby-talk\
**Created:** [8 September 2009 16:38 UTC](https://rubytalk.org/t/time-based-loop-limiting/55298 "2009-09-08T16:38:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Tomer](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@Michael\_Tomer](https://rubytalk.org/u/Michael_Tomer)\
**Post date:** [8 September 2009 16:38 UTC](https://rubytalk.org/t/time-based-loop-limiting/55298/1 "2009-09-08T16:38:33Z")

</div>

I'm trying to write a loop that will only run a certain number of times  
per second. For instance, in Ruby-Processing, you would use  
"frame\_rate(30)" to make the main loop execute no more than 30 evenly  
spaced frames per second. How might I go about doing something like  
that?

I could try calling sleep with a float that represents the difference  
between the next time I want the loop to run and the current time. That  
seems clunky, though.

I also thought about using the Observer pattern to have a timer alert  
the simulation each time it should update. That also seems wrong.

I feel like I must be overlooking something simple. Any ideas?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Paul\_Smith1](https://avatars.discourse-cdn.com/v4/letter/p/5daacb/32.png) [@Paul\_Smith1](https://rubytalk.org/u/Paul_Smith1)\
**Post date:** [8 September 2009 16:44 UTC](https://rubytalk.org/t/time-based-loop-limiting/55298/2 "2009-09-08T16:44:50Z")

</div>

I think your first method is the one I've seen the most in game loops.  
You calculate how long you should sleep based on what time it is now  
and what time you want to wake up, being careful that your last frame  
could have taken over 1/30th of a second.

You also use the elapsed time in your physics, so the physics engine  
is not tied to your frame rate.

> **···**
>
> On Tue, Sep 8, 2009 at 5:38 PM, Michael Tomer\<michael.tomer@fnis.com\> wrote:
> 
> > I'm trying to write a loop that will only run a certain number of times  
> > per second. For instance, in Ruby-Processing, you would use  
> > "frame\_rate(30)" to make the main loop execute no more than 30 evenly  
> > spaced frames per second. How might I go about doing something like  
> > that?
> > 
> > I could try calling sleep with a float that represents the difference  
> > between the next time I want the loop to run and the current time. That  
> > seems clunky, though.
> > 
> > I also thought about using the Observer pattern to have a timer alert  
> > the simulation each time it should update. That also seems wrong.
> > 
> > I feel like I must be overlooking something simple. Any ideas?  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> --  
> Paul Smith  
> [http://www.nomadicfun.co.uk](http://www.nomadicfun.co.uk)
> 
> paul@pollyandpaul.co.uk

---

<div class="post-metadata">

**Author:** ![Bertram\_Scharpf](https://avatars.discourse-cdn.com/v4/letter/b/43a26b/32.png) [@Bertram\_Scharpf](https://rubytalk.org/u/Bertram_Scharpf)\
**Post date:** [8 September 2009 22:14 UTC](https://rubytalk.org/t/time-based-loop-limiting/55298/3 "2009-09-08T22:14:46Z")

</div>

Hi,

> I'm trying to write a loop that will only run a certain number of times  
> per second.

The proper way to do this is to call the setitimer(2) function  
that will send SIGALRM periodically to the process. It has been  
discussed a long time ago:

&nbsp;&nbsp;[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/130989](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/130989)

Still I cannot find a setitimer mapping in the Ruby interpreter.  
So I wrote my own:

&nbsp;&nbsp;[http://bertram-scharpf.homelinux.com:8808/doc\_root/itimer-1.0/rdoc/index.html](http://bertram-scharpf.homelinux.com:8808/doc_root/itimer-1.0/rdoc/index.html)

Bertram

> **···**
>
> Am Mittwoch, 09. Sep 2009, 01:38:33 +0900 schrieb Michael Tomer:
> 
> --  
> Bertram Scharpf  
> Stuttgart, Deutschland/Germany  
> [http://www.bertram-scharpf.de](http://www.bertram-scharpf.de)

---

<div class="post-metadata">

**Author:** ![Michael\_Tomer](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@Michael\_Tomer](https://rubytalk.org/u/Michael_Tomer)\
**Post date:** [8 September 2009 17:17 UTC](https://rubytalk.org/t/time-based-loop-limiting/55298/4 "2009-09-08T17:17:12Z")

</div>

Thanks, Paul. I feel better knowing that my first guess was right, even  
though it initially struck me as wrong. I guess that'll teach me to  
second guess myself.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Brabuhr](https://avatars.discourse-cdn.com/v4/letter/b/919ad9/32.png) [@Brabuhr](https://rubytalk.org/u/Brabuhr)\
**Post date:** [9 September 2009 03:11 UTC](https://rubytalk.org/t/time-based-loop-limiting/55298/5 "2009-09-09T03:11:49Z")

</div>

Took a shot with FFI:

require 'ffi'

module LibC  
&nbsp;&nbsp;extend FFI::Library

&nbsp;&nbsp;class Timeval \< FFI::Struct  
&nbsp;&nbsp;&nbsp;&nbsp;layout :tv\_sec, :long,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:tv\_usec, :long  
&nbsp;&nbsp;end

&nbsp;&nbsp;class Itimerval \< FFI::Struct  
&nbsp;&nbsp;&nbsp;&nbsp;layout :it\_interval, Timeval,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:it\_value, Timeval  
&nbsp;&nbsp;end

&nbsp;&nbsp;attach\_function :getitimer, [:int, :pointer], :int  
&nbsp;&nbsp;attach\_function :setitimer, [:int, :pointer, :pointer], :int  
end

Signal.trap("SIGALRM") do  
&nbsp;&nbsp;puts "SIGALRM: #{Time.now}"  
end

tv = LibC::Timeval.new  
tv[:tv\_sec] = 2  
tv[:tv\_usec] = 0

puts Time.now  
LibC::setitimer(0, tv, nil)  
sleep 15  
puts Time.now

#=\>  
# Wed Sep 09 02:57:18 +0000 2009  
# SIGALRM: Wed Sep 09 02:57:20 +0000 2009  
# SIGALRM: Wed Sep 09 02:57:22 +0000 2009  
# SIGALRM: Wed Sep 09 02:57:24 +0000 2009  
# SIGALRM: Wed Sep 09 02:57:26 +0000 2009  
# SIGALRM: Wed Sep 09 02:57:28 +0000 2009  
# SIGALRM: Wed Sep 09 02:57:30 +0000 2009  
# SIGALRM: Wed Sep 09 02:57:32 +0000 2009  
# SIGALRM: Wed Sep 09 02:57:33 +0000 2009  
# Wed Sep 09 02:57:33 +0000 2009

> **···**
>
> On Tue, Sep 8, 2009 at 6:14 PM, Bertram Scharpf\<lists@bertram-scharpf.de\> wrote:
> 
> > Am Mittwoch, 09. Sep 2009, 01:38:33 +0900 schrieb Michael Tomer:
> > 
> > > I'm trying to write a loop that will only run a certain number of times  
> > > per second.
> > 
> > The proper way to do this is to call the setitimer(2) function  
> > that will send SIGALRM periodically to the process. It has been  
> > discussed a long time ago:
> > 
> > [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/130989](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/130989)
> > 
> > Still I cannot find a setitimer mapping in the Ruby interpreter.  
> > So I wrote my own:
> > 
> > [http://bertram-scharpf.homelinux.com:8808/doc\_root/itimer-1.0/rdoc/index.html](http://bertram-scharpf.homelinux.com:8808/doc_root/itimer-1.0/rdoc/index.html)

---

<div class="post-metadata">

**Author:** ![Mike\_B](https://avatars.discourse-cdn.com/v4/letter/m/977dab/32.png) [@Mike\_B](https://rubytalk.org/u/Mike_B)\
**Post date:** [9 September 2009 04:05 UTC](https://rubytalk.org/t/time-based-loop-limiting/55298/6 "2009-09-09T04:05:27Z")

</div>

How would one make use of this?

> **···**
>
> On Sep 8, 2:14 pm, Bertram Scharpf \<li...@bertram-scharpf.de\> wrote:
> 
> > Hi,
> > 
> > Am Mittwoch, 09. Sep 2009, 01:38:33 +0900 schrieb Michael Tomer:
> > 
> > \> I'm trying to write a loop that will only run a certain number of times  
> > \> per second.
> > 
> > The proper way to do this is to call the setitimer(2) function  
> > that will send SIGALRM periodically to the process. It has been  
> > discussed a long time ago:
> > 
> > [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/130989](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/130989)
> > 
> > Still I cannot find a setitimer mapping in the Ruby interpreter.  
> > So I wrote my own:
> > 
> > [http://bertram-scharpf.homelinux.com:8808/doc\_root/itimer-1.0/rdoc/in\.\.\](http://bertram-scharpf.homelinux.com:8808/doc_root/itimer-1.0/rdoc/in%5C.%5C.%5C).
> > 
> > Bertram
> > 
> > --  
> > Bertram Scharpf  
> > Stuttgart, Deutschland/Germanyhttp://www.bertram-scharpf.de

---

<div class="post-metadata">

**Author:** ![Bertram\_Scharpf](https://avatars.discourse-cdn.com/v4/letter/b/43a26b/32.png) [@Bertram\_Scharpf](https://rubytalk.org/u/Bertram_Scharpf)\
**Post date:** [9 September 2009 07:19 UTC](https://rubytalk.org/t/time-based-loop-limiting/55298/7 "2009-09-09T07:19:02Z")

</div>

Hi,

> \>  
> \> \> I'm trying to write a loop that will only run a certain number of times  
> \> \> per second.  
> \>  
> \> Still I cannot find a setitimer mapping in the Ruby interpreter.  
> \> So I wrote my own:  
> \>  
> \> [http://bertram-scharpf.homelinux.com:8808/doc\_root/itimer-1.0/rdoc/in\.\.\](http://bertram-scharpf.homelinux.com:8808/doc_root/itimer-1.0/rdoc/in%5C.%5C.%5C).
> 
> How would one make use of this?

Argh. I wonder how that could happen. The link is of course:  
&nbsp;&nbsp;  
&nbsp;&nbsp;[http://bertram-scharpf.homelinux.com:8808/doc\_root/itimer-1.0/rdoc/index.html](http://bertram-scharpf.homelinux.com:8808/doc_root/itimer-1.0/rdoc/index.html)

Install the gem. You can also use this gem source:

&nbsp;&nbsp;[http://gems.bertram-scharpf.de](http://gems.bertram-scharpf.de)

Then (untested)

&nbsp;&nbsp;require "itimer"  
&nbsp;&nbsp;trap "SIGALRM" do puts Time.now end  
&nbsp;&nbsp;Process.setitimer 2

This puts out the time every two seconds.

Bertram

> **···**
>
> Am Mittwoch, 09. Sep 2009, 13:05:27 +0900 schrieb Mike Barsalou:
> 
> > On Sep 8, 2:14 pm, Bertram Scharpf \<li...@bertram-scharpf.de\> wrote:  
> > \> Am Mittwoch, 09. Sep 2009, 01:38:33 +0900 schrieb Michael Tomer:
> 
> --  
> Bertram Scharpf  
> Stuttgart, Deutschland/Germany  
> [http://www.bertram-scharpf.de](http://www.bertram-scharpf.de)

---

<div class="post-metadata">

**Author:** ![Michael\_Tomer](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@Michael\_Tomer](https://rubytalk.org/u/Michael_Tomer)\
**Post date:** [9 September 2009 12:51 UTC](https://rubytalk.org/t/time-based-loop-limiting/55298/8 "2009-09-09T12:51:36Z")

</div>

Wow, thanks for the SIGALRM binding. That should do quite nicely!

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
