Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to pause 10ms in perl

Status
Not open for further replies.

fz315

Programmer
Oct 29, 2004
6
CA
Hi:
anyone know how to pause 10ms in perl script? sleep command just pause seconds.
Thanks a lot
 
Check out the Time::HiRes module. I have not used this, but sounds like it will do what you want.

perldoc Time::HiRes
 
Here's another method I recently found in O'Reilly's Perl Cookbook:

Use the select() function, if your system supports it:

select(undef, undef, undef, $time_to_sleep);

 
try

my $time = 10/1000;
....do something....
sleep $time;

but if you put that you will not see a difference cause the time that seelps is les than a second

Anyway good luck.

 
But out of curiosity, how would you see that it did pause for 10ms (it is less than a f** sec)?
Anyway the thing i gave you, can be test if you set $time = 100/10; just to see that it works, this will pause it for ten seconds, after relising that 'yes it does work' set '$time = 10/1000; and see the difference "Quick EYE"!
 
The sleep function only works with integers. I don't know the exact internals of it but it's as if it runs the argument through the int function first (i.e. rounds it down to the previous integer value). Time::HiRes would be the way to go. If you've used something like "sleep 0.01", just add the following line to the top of your script:
Code:
use Time::HiRes qw( sleep );
That imports the Time::HiRes::sleep() function which replaces the builtin sleep function.

perluserpengo - both the builtin sleep() and Time::HiRes::sleep() return the number of seconds it slept for. e.g.:
Code:
print sleep 0.999;
print Time::HiRes::sleep 0.999;
 
what do you mean?
you mean it doesn't work the way i wrote it?
 
oh i see..
sleep round the time 0.5->0 , 1.5->1
ok my bad.

But the Time::HiRes::sleep gives multiple results if you test it a lot of times
for $time = 3/2 it gives
1.498657
1.498611
1.498654
1.514205
1.498548
and as many times you check you gonna get a different number,
do you know why this is happening?
 
>> you mean it doesn't work the way i wrote it?
Yes. 10/1000 is 0.01

The built-in sleep() function uses the integer part only, which is 0 and therefore doesn't sleep at all.
 
pengo,

I'd suggest that 1.5 can't be taken as the only thing the CPU will recognise, as there are other processes running on the machine, and these need to be scheduled, but they are within 1.5% (1.42% actually) of the intended time, and mostly lower.

If you used a finer grain, the results might be closer to what is expected.

I'd further suggest that for any sleep function to work as expected it would need to be the only process on the machine, including the OS, but I could be talking out of my a** ;-)

--Paul


cigless ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top