[PHP] How to reduce CPU usage in a cycle? [ag]

2003-12-11 Thread TauAlex
Hello!

I'm having a problem with my php script on my linux web server. In that
script there is a cycle with some math calculations (about ~30 steps), the
execution time is less 1 second, but the script loads the server CPU up to
90%...
I've optimized script already to the minimum math function, anyway, it still
overloads the CPU.
How can I reduce the CPU load? It seems that usleep() does not help much,
especially in Windows (it's not supported).
How can I give system a portion of time in the end of each step (I don't
mind if it will increase the job time)? Is there a solution for both Windows
and Linux?

For instance, in C++ under Windows I would do this:

do
{
[... math calculations ...]
while(::PeekMessage(message,NULL,0,0,PM_NOREMOVE))
if (!theApp.PumpMessage()) break;
} while(!quit);

This gives system to redraw screen, process any other events during long
cycles...
Is there similar approach in PHP for web server programming?

p.s. Would be nice to know other techniques to reduce CPU load ;)

Thank you!

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] How to reduce CPU usage in a cycle? [ag]

2003-12-11 Thread Brent Baisley
Personally, I wouldn't worry about it unless it is actually interfering 
with other processes. The OS will give any process or thread 100% of 
the CPU if there is nothing else that needs the CPU. What does you 
context switching numbers look like? If there is a lot of context 
switching, then your processes or threads are getting kicked off the 
CPU before they can accomplish much. And that's a problem.

The duration of your script is less  than a second, so that's not 
really a long enough time have an affect on other parts of the system. 
Heck, it may even being completing within just a couple of time slices 
(the length of time the OS allows a process/thread to use the CPU).

You could always adjust the priority level (nice value in Unix) of 
whatever PHP is running under (i.e. Apache). Although you should 
understand what you are doing if you are adjusting nice values.

On Dec 11, 2003, at 10:03 AM, TauAlex wrote:

Hello!

I'm having a problem with my php script on my linux web server. In that
script there is a cycle with some math calculations (about ~30 steps), 
the
execution time is less 1 second, but the script loads the server CPU 
up to
90%...
I've optimized script already to the minimum math function, anyway, it 
still
overloads the CPU.
How can I reduce the CPU load? It seems that usleep() does not help 
much,
especially in Windows (it's not supported).
How can I give system a portion of time in the end of each step (I 
don't
mind if it will increase the job time)? Is there a solution for both 
Windows
and Linux?

--
Brent Baisley
Systems Architect
Landover Associates, Inc.
Search  Advisory Services for Advanced Technology Environments
p: 212.759.6400/800.759.0577
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] How to reduce CPU usage in a cycle? [ag]

2003-12-11 Thread Jas
Well have you tried to define one function to include the for all 
variables that require mt_rand()?
Jas

Brent Baisley wrote:

Personally, I wouldn't worry about it unless it is actually interfering 
with other processes. The OS will give any process or thread 100% of the 
CPU if there is nothing else that needs the CPU. What does you context 
switching numbers look like? If there is a lot of context switching, 
then your processes or threads are getting kicked off the CPU before 
they can accomplish much. And that's a problem.

The duration of your script is less  than a second, so that's not really 
a long enough time have an affect on other parts of the system. Heck, it 
may even being completing within just a couple of time slices (the 
length of time the OS allows a process/thread to use the CPU).

You could always adjust the priority level (nice value in Unix) of 
whatever PHP is running under (i.e. Apache). Although you should 
understand what you are doing if you are adjusting nice values.

On Dec 11, 2003, at 10:03 AM, TauAlex wrote:

Hello!

I'm having a problem with my php script on my linux web server. In that
script there is a cycle with some math calculations (about ~30 steps), 
the
execution time is less 1 second, but the script loads the server CPU 
up to
90%...
I've optimized script already to the minimum math function, anyway, it 
still
overloads the CPU.
How can I reduce the CPU load? It seems that usleep() does not help much,
especially in Windows (it's not supported).
How can I give system a portion of time in the end of each step (I don't
mind if it will increase the job time)? Is there a solution for both 
Windows
and Linux?

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] How to reduce CPU usage in a cycle? [ag]

2003-12-11 Thread Jas
ex.
?php
function rand($attack,$skills,$damage,$armor) {
$do = array($attack,$skills,$damage,$armor);
finish=false	
while (!finish) {
	$do = mt_rand(mktime(microtime(1,100)); }
while($do = list($attack,$skills,$damage,$armor)) {
	calculate total damage for opponent }
}
?
I read that the use of mt_rand will produce the same results each time 
the page is called with a certain number... so I would recommend using 
microtime in conjunction with your mt_rand calls to ensure true random 
numbers.
By throwing your vars into an array and looping through it applying your 
random points deduction then listing the contents of the array (with new 
values) it might improve cpu performance but then again. I am still 
fairly new to php.
Jas

Jas wrote:

Well have you tried to define one function to include the for all 
variables that require mt_rand()?
Jas

Brent Baisley wrote:

Personally, I wouldn't worry about it unless it is actually 
interfering with other processes. The OS will give any process or 
thread 100% of the CPU if there is nothing else that needs the CPU. 
What does you context switching numbers look like? If there is a lot 
of context switching, then your processes or threads are getting 
kicked off the CPU before they can accomplish much. And that's a problem.

The duration of your script is less  than a second, so that's not 
really a long enough time have an affect on other parts of the system. 
Heck, it may even being completing within just a couple of time slices 
(the length of time the OS allows a process/thread to use the CPU).

You could always adjust the priority level (nice value in Unix) of 
whatever PHP is running under (i.e. Apache). Although you should 
understand what you are doing if you are adjusting nice values.

On Dec 11, 2003, at 10:03 AM, TauAlex wrote:

Hello!

I'm having a problem with my php script on my linux web server. In that
script there is a cycle with some math calculations (about ~30 
steps), the
execution time is less 1 second, but the script loads the server CPU 
up to
90%...
I've optimized script already to the minimum math function, anyway, 
it still
overloads the CPU.
How can I reduce the CPU load? It seems that usleep() does not help 
much,
especially in Windows (it's not supported).
How can I give system a portion of time in the end of each step (I don't
mind if it will increase the job time)? Is there a solution for both 
Windows
and Linux?

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php