[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

10 millisecond delay/callback

Earle Clubb

4/26/2007 7:57:00 PM

I need to perform a task every 10ms. I've been using
kernel.sleep(0.01), but sleep seems to be slow and erratic at sub-second
resolutions. Here's a test I ran along with the results:

irb(main):001:0> 0.upto(25) do
irb(main):002:1* puts Time.now.usec/1000
irb(main):003:1> STDOUT.flush
irb(main):004:1> sleep 0.01
irb(main):005:1> end
105
132
143
162
191
205
219
238
257
277
294
311
330
354
366
385
412
431
450
468
488
504
522
542
556
581
=> 0
irb(main):006:0>


So I'm wondering if there's a way to get a steady 10ms delay. Another
option would be to set up an alarm or scheduled callback or something
every 10ms. Is this possible.

Thanks,
Earle

--
Posted via http://www.ruby-....

28 Answers

khaines

4/26/2007 8:07:00 PM

0

Earle Clubb

4/26/2007 9:02:00 PM

0

unknown wrote:
> On Fri, 27 Apr 2007, Earle Clubb wrote:
>
>> 132
>> 143
>> 162
>> 191
>
> Try select()
> ----
> irb(main):005:0> 0.upto(25) do
> irb(main):006:1* select(nil,nil,nil,0.01)
> irb(main):007:1> puts Time.now.usec/1000
> irb(main):008:1> end
> 138
> 148
> 158
> 168

I just tried select and had the same results as sleep:

irb(main):001:0> 25.times do
irb(main):002:1* select(nil,nil,nil,0.01)
irb(main):003:1> puts Time.now.usec/1000
irb(main):004:1> end
787
797
814
839
858
877
894
912
932
951
965
985
1
21
39
62
81
100
117
134
154
174
193
212
228


FWIW, I'm using ruby 1.8.5 on Fedora Core 6 with kernel
2.6.20-1.2944.fc6.

Earle


--
Posted via http://www.ruby-....

Ganesh Gunasegaran

4/26/2007 9:19:00 PM

0

Are you running Fedora inside a virtual machine?

Virtual machines like VMware, qemu are known to cause clock skews!

Cheers,
Ganesh Gunasegaran.

On 27-Apr-07, at 2:32 AM, Earle Clubb wrote:

> unknown wrote:
>> On Fri, 27 Apr 2007, Earle Clubb wrote:
>>
>>> 132
>>> 143
>>> 162
>>> 191
>>
>> Try select()
>> ----
>> irb(main):005:0> 0.upto(25) do
>> irb(main):006:1* select(nil,nil,nil,0.01)
>> irb(main):007:1> puts Time.now.usec/1000
>> irb(main):008:1> end
>> 138
>> 148
>> 158
>> 168
>
> I just tried select and had the same results as sleep:
>
> irb(main):001:0> 25.times do
> irb(main):002:1* select(nil,nil,nil,0.01)
> irb(main):003:1> puts Time.now.usec/1000
> irb(main):004:1> end
> 787
> 797
> 814
> 839
> 858
> 877
> 894
> 912
> 932
> 951
> 965
> 985
> 1
> 21
> 39
> 62
> 81
> 100
> 117
> 134
> 154
> 174
> 193
> 212
> 228
>
>
> FWIW, I'm using ruby 1.8.5 on Fedora Core 6 with kernel
> 2.6.20-1.2944.fc6.
>
> Earle
>
>
> --
> Posted via http://www.ruby-....
>


Earle Clubb

4/26/2007 9:37:00 PM

0

Ganesh Gunasegaran wrote:
> Are you running Fedora inside a virtual machine?
>
> Virtual machines like VMware, qemu are known to cause clock skews!
>
> Cheers,
> Ganesh Gunasegaran.

Nope. No VM here.

Earle

--
Posted via http://www.ruby-....

Joel VanderWerf

4/26/2007 10:04:00 PM

0

khaines@enigo.com wrote:
...
> Try select()
> ----
> irb(main):005:0> 0.upto(25) do
> irb(main):006:1* select(nil,nil,nil,0.01)
> irb(main):007:1> puts Time.now.usec/1000
> irb(main):008:1> end
> 138
> 148
> 158
> 168
> 178
> 188
> 198
...

What kind of system are you on? It doesn't work so well for me.

irb(main):017:0> [~] uname -a
Linux tumbleweed 2.6.20-15-generic #2 SMP Sun Apr 15 07:36:31 UTC 2007
i686 GNU/Linux
[~] ruby -v
ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-linux]

rb(main):013:0> 0.upto(25) do
irb(main):014:1* select(nil,nil,nil,0.01)
irb(main):015:1> puts Time.now.usec/1000
irb(main):016:1> end
240
250
262
274
286
298
310
322
334
347
358
370
382
398
406
418
430
442
455
466
478
490
503
514
526
538


--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Gavin Kistner

4/26/2007 10:08:00 PM

0

On Apr 26, 2:06 pm, khai...@enigo.com wrote:
> On Fri, 27 Apr 2007, Earle Clubb wrote:
> > I need to perform a task every 10ms. I've been using
> > kernel.sleep(0.01), but sleep seems to be slow and erratic at sub-second
> > resolutions. Here's a test I ran along with the results:
[snip]
> Try select()
[snip]

No such glory on Windows:

irb(main):001:0> 0.upto(25) do
irb(main):002:1* select(nil,nil,nil,0.01)
irb(main):003:1> puts Time.now.usec/1000
irb(main):004:1> end
619
634
650
650
666
681
681
697
712
712
[snip]
=> 0
irb(main):005:0>

C:\Documents and Settings\gavin.kistner>ruby -v
ruby 1.8.5 (2006-08-25) [i386-mswin32]

Lionel Bouton

4/27/2007 12:59:00 AM

0

Joel VanderWerf wrote the following on 27.04.2007 00:04 :
> khaines@enigo.com wrote:
> ...
>> Try select()
>> ----
>> irb(main):005:0> 0.upto(25) do
>> irb(main):006:1* select(nil,nil,nil,0.01)
>> irb(main):007:1> puts Time.now.usec/1000
>> irb(main):008:1> end
>> 138
>> 148
>> 158
>> 168
>> 178
>> 188
>> 198
> ...
>
> What kind of system are you on? It doesn't work so well for me.

I'm wondering too, I see a nearly constant 2ms delay added to the 10ms
sleep with select on both an Athlon64 X2 3800+ and an Athlon XP 2400+.

Is there some YARV in there?

Lionel

John Joyce

4/27/2007 5:02:00 AM

0


On Apr 27, 2007, at 9:58 AM, Lionel Bouton wrote:

> Joel VanderWerf wrote the following on 27.04.2007 00:04 :
>> khaines@enigo.com wrote:
>> ...
>>> Try select()
>>> ----
>>> irb(main):005:0> 0.upto(25) do
>>> irb(main):006:1* select(nil,nil,nil,0.01)
>>> irb(main):007:1> puts Time.now.usec/1000
>>> irb(main):008:1> end
>>> 138
>>> 148
>>> 158
>>> 168
>>> 178
>>> 188
>>> 198
>> ...
>>
>> What kind of system are you on? It doesn't work so well for me.
>
> I'm wondering too, I see a nearly constant 2ms delay added to the 10ms
> sleep with select on both an Athlon64 X2 3800+ and an Athlon XP 2400+.
>
> Is there some YARV in there?
>
> Lionel
>
Have you all tried this outside of irb?
Just run the actual script itself + profiler to find out where the
slowdown is occurring.
Try it with sleep and select versions. This might show some different
points.. maybe not.

Evan Webb

4/27/2007 6:45:00 AM

0

It's unlikely to get 10ms intervals reliably even in C, so getting it
in ruby is very unlikely.

The reason it's hard to get it even in C is (as man setitimer says):
"Time values smaller than the resolution of the system clock are
rounded up to this resolution (typically 10 milliseconds)."

Even taking the system clock resolution out of the equation, if the GC
kicks in, it will definitely cause a delay greater than 10ms. Add in
the rest of the delays caused by ruby's runtime, and it's a loosing
game.

If you require running a loop at 10ms intervals, you're probably going
to have to dip down into C.

- Evan Phoenix

On Apr 26, 12:57 pm, Earle Clubb <ecl...@valcom.com> wrote:
> I need to perform a task every 10ms. I've been using
> kernel.sleep(0.01), but sleep seems to be slow and erratic at sub-second
> resolutions. Here's a test I ran along with the results:
>
> irb(main):001:0> 0.upto(25) do
> irb(main):002:1* puts Time.now.usec/1000
> irb(main):003:1> STDOUT.flush
> irb(main):004:1> sleep 0.01
> irb(main):005:1> end
> 105
> 132
> 143
> 162
> 191
> 205
> 219
> 238
> 257
> 277
> 294
> 311
> 330
> 354
> 366
> 385
> 412
> 431
> 450
> 468
> 488
> 504
> 522
> 542
> 556
> 581
> => 0
> irb(main):006:0>
>
> So I'm wondering if there's a way to get a steady 10ms delay. Another
> option would be to set up an alarm or scheduled callback or something
> every 10ms. Is this possible.
>
> Thanks,
> Earle
>
> --
> Posted viahttp://www.ruby-....


Michael W. Ryder

4/27/2007 8:08:00 AM

0

John Joyce wrote:
>
> On Apr 27, 2007, at 9:58 AM, Lionel Bouton wrote:
>
>> Joel VanderWerf wrote the following on 27.04.2007 00:04 :
>>> khaines@enigo.com wrote:
>>> ...
>>>> Try select()
>>>> ----
>>>> irb(main):005:0> 0.upto(25) do
>>>> irb(main):006:1* select(nil,nil,nil,0.01)
>>>> irb(main):007:1> puts Time.now.usec/1000
>>>> irb(main):008:1> end
>>>> 138
>>>> 148
>>>> 158
>>>> 168
>>>> 178
>>>> 188
>>>> 198
>>> ...
>>>
>>> What kind of system are you on? It doesn't work so well for me.
>>
>> I'm wondering too, I see a nearly constant 2ms delay added to the 10ms
>> sleep with select on both an Athlon64 X2 3800+ and an Athlon XP 2400+.
>>
>> Is there some YARV in there?
>>
>> Lionel
>>
> Have you all tried this outside of irb?
> Just run the actual script itself + profiler to find out where the
> slowdown is occurring.
> Try it with sleep and select versions. This might show some different
> points.. maybe not.
>

I tried your script on Windows under irb and using ruby and get similar
results. I end up with times like 171.0, 171.0, 187.0, 203.0,
203.0,..., obviously select is not waiting, at least under Windows.