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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need accurate loop time delay in microseconds 1

Status
Not open for further replies.

mastermagrath

Technical User
May 21, 2004
28
0
0
US
Hi all,

wonder an you help.
I run active state perl 5.8 on my windows machine.
I need to control the increment rate of a while loop and after trying with Time::HiRes usleep and Win32::Sleep i've found something that puzzled me and led me to do some benchmark testing. This then showed a problem with those sleep functions. For example, usleep works at the millisecond level:

while (1) {
$actualSleepTime = usleep (10);
print "Actual sleep time = $actualSleepTime\n";
}

This works fine and gives roughly an actual sleep time of ~10us.
However if you increase the sleep value to any value up to 999 it still sleeps for 10us.
With a sleep time of 1000 i then get an actual sleep time of around 9000us! And this actual time remains up until the value given is well above 10000 wherein the actual sleep time is still way out.
Does anyone know what is going on here? If it was to do with granularity of the clock then i dont understand how i can achieve a 10us time.
I've even tried adopting the while loop so that it sums the actual sleep times and exits when it gets to a predetermined time but this then seems to result in inaccurate real time e.g.:

$starttime = scalar gettimeofday;

while ($timeslept < 2000000) {
$timeslept += usleep (10);
#print "time slept = $timeslept\n";
}

$stoptime = scalar gettimeofday;
$elapsed = $stoptime - $starttime;
print "Elapsed = $elapsed\n";

Please, please, please can someone give some advice as to how i can achieve what i need accurately otherwise a rather large script i have spent weeks on will be useless.

Essentially the script allows a user to select how many packets per second he/she wishes to send via udp so that the throughput can be controlled.
I love perl but recently i've hit upon a few things that are making me think of turning my attention to C++ and i dont want to do this!!!

Thanks in advance
 
I'm not sure but this may have something to do with other processes running on the machine and waiting for control to be returned to the perl process, try setting the priority of the perl process to Realtime in the task manager, and see if it gets closer to your expectations.

If what I'm thinking is right, looks like you're going to be in the same boat with C/C++ as well

HTH
--Paul

Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
I'll look into that .... thanks
By the way, there is a typo in my message, usleep works at a granularity of microseconds not milli....anyway doesn't change the original problem i've outlined....
 
Mastermagrath,

You must need to control this very accurately if calling usleep(10) repeatedly using a loop doesn't give you the control you need.

I remember doing some assembly programming on an X86 box (previous life, don't ask) and the clock ticked 18.2 times a second in those far-off days which meant that the best granularity you could get was an 18th of a second, or thereabouts.

Are you running into the same limit?

How accurately do you need to be able to control the throughput?

Mike

I am not inscrutable. [orientalbow]

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top