It’s not the first time I get a too much cpu load warning from my hosting. The code is just some random php script with mysql queries, nothing fancy. (THe tables are nothing extraordinary, a few hundred lines maximum and I always limit them if requested.
I don’t mind if it runs for 0.15 second instead of 0.05, so is there a way I can control process priority or limit cpu load?
Thanks!
Answer
If this is a dameon or program that runs for long time add sleep()
/usleep()
. A small sleep will reduce your CPU usage dramatically.
Following code will consume a lot of cpu
while(...){ //do stuff }
Because you are not giving room for CPU to do other task here. change it to
while(...){ //do stuff sleep(1); }
This will greatly decrease your CPU usage. 1 second for CPU is a lot of time to do other task.
To sleep that extra 0.1
second (0.15 - 0.05
) use usleep().
usleep(100000);