views:

170

answers:

2

Hi guys I have a weird question,

I have a cli php script runs on Centos 5.x which uses usleep (somtimes 1sec, sometimes 2sec, somtimes 100ms it depends) if there is some wait required, but what I have noticed its that once on usleep() it seems to use about 40% of idle CPU:

Cpu(s):  5.3%us, 21.3%sy,  0.0%ni, 57.2%id,  0.0%wa,  0.0%hi,  0.0%si, 16.1%st

any ideas ?
cheers

+2  A: 

Under Windows systems, if you do not set the time limit of the execution of your script to 0 (set_time_limit(0);), the php executable will eat as much as 50% CPU power when using sleep or usleep functions.

celalo
OMG, why is that?
jpalecek
I have tried that, no difference... cheers P.S. i am on Linux Centos 5..
Marcin
+2  A: 

This doesn't happen for me with a very simple testcase. Try the following on your system to see if you still get the excessive CPU time.

Script test.php:

<?php
for ($n=0;$n<1000;$n++)
{
  usleep(10);
}
?>

Then at the command line run: time php test.php

My results are as follows:

[ar@arctic ~]$ cat /etc/redhat-release 
CentOS release 5.2 (Final)
[ar@arctic ~]$ time php test.php 

real    0m1.020s
user    0m0.013s
sys     0m0.006s

You can see that the user and sys time are very very small in comparison to the real (or elapsed) time. i.e. CPU utilisation was very low.

ar
here you go my times= real: 0m4.941s, user: 0m0.036s, sys: 0m0.036s. but how this one is realeted to the CPU usage anyway ?
Marcin
From your results, I would conclude that `usleep` is not your problem. Can you show us what else is in your code around usleep - I presume you are in some sort of loop.
ar