mastermagrath
Technical User
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
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