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

reducing cpu & memory usage

Status
Not open for further replies.

mwpclark

Programmer
Mar 14, 2005
59
0
0
US
Hello

I am seeking suggestions to reduce cpu and memory usage for a couple of code snippets. I have been adding @ because of a large number of undefined variable notices. However I am coming to realize that this is expensive on a busy server.

The first is for an incoming variable:
Code:
$state = @$_GET['state'];
$city = @$_GET['city'];
$name = @$_GET['name'];

The second is for mysql:
Code:
$id = @$row['id'];
$name = @$row['name'];
$address = @$row['address'];

In some cases I am using isset to check if the variable has been defined. I have been researching ternary operators, but do not exactly understand how to apply them.

In most cases, if the variable is not defined (i.e., not present in the call or database), then the default value could be nothing or null, as long as it is defined and does not cause a notice later.

I have recently moved to a new box, and I **think** on the previous box zend optimizer was obscuring this cpu/memory demand. However for some conflict I have not gotten it to install on the new box. Still working on that front also, but this is really a php question.

Thanks & Cheers
Mike
 
for calls to mysql, test the result of the query. if it is false you have an error. if not then the fields that you have selected in the query should always be supplied. there is no need to prepend an @

Code:
$result = mysql_query("select * from table");
if (!$result) bailoutgracefully();
while ($row = mysql_fetch_assoc($result)):
//do something
endwhile;

for 'dimensioning' variables there are two approaches to consider. I use the former inside functions and classes when I am uncertain as to what data will be passed

Code:
class foo{
 public $var1 = '';
 public $var2 = '';
 public $var3 = 'some default';
 public $fields = array('var1','var2','var3');
 public function __construct($data){
   foreach ($this->fields as $field){
     if (!empty($data[$field])) $this->$field = $data[$field]
   }
 }
}
$obj = new foo(array('var1'=>'hello'));
print_r($obj);

or for a simple function
Code:
function test ($array=array()){
	$default   = array('var1'=>'','var2'=>0,'var3'=>'some default value');
	$array = array_merge($default, $array);
	return $array;
}
print_r(test(array('var1'=>"hello")	));

and for things like testing query vars use the ternary operator

Code:
$var = !empty($_GET['var1']) ? $_GET['var1'] : '';

the ternary operator works like this

Code:
$result = (test) ? (value if test is true) : (value if test is false);

however, production code should not display errors to the user. you should log the errors and ensure that there are error traps to bail out gracefully. error_display should always be set to off.

and more to the point I would think it is very unlikely that simply prepending an @ sign to a bunch of lines would cause memory bottle necks or CPU drain. Particularly not for something trivial like sql and/or query vars issues.

If you are genuinely suffering from excess CPU load then this will be coming from another source. Memory problems might be related to sql, but not to the @ signs. If you are returning very large column sizes within a data set then you might be busting your memory limit. you can check this my looking at
Code:
memory_get_usage();
and
memory_get_peak_usage();

 
Thank you jpadie, your explanation of ternary operators is very clear. What I am looking at is high httpd load, a lot of which is php calling either sql or oracle or both, as well as interpreting xml from third party api's.

The latest google update seems to have sent me more traffic and this is exacerbating the problem. Be careful what you wish for!

Looking at top, ps aux, mysql showprocesslist, netstat, access_log and error_log, I don't see oracle or mysql causing problems. I do see lots of httpd. This is why I am attempting to tune some of these functions, and at least postpone more server investment.

I have been gradually chipping away at the undefined index and undefined variable notices, really there are very few warnings or errors. And I am looking at what can be cached.

Thanks again
Mike
 
Can you site scale out i.e. add more servers.
Also is your database on the same machine as the PHP ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top