Performance by Code review



1. Make cache as possible

Caching is a possible support to reduce your CPU cycle. However, a good developer knows that most function for the cache is not persistent for data. So It should be considered that application should work with or without caching.


function chachtest() 
{
sleep(10);
return date('d/m/Y H:i:s');
}
$set1 = chachtest();
echo $set1;

This code will run for ten Second , just because use of the sleep function, to simulate one slow operation. let re-analysis this code to:

$memcache = new Memcache;
$memcache->connect('localhost', 11211);
function chachtest() {
sleep(1);
return date('d/m/Y H:i:s');
}
$set1 = $memcache->get('item');
if ($set1 === false) {
$set1 = chachtest();
$memcache->set('item', $set1);
}
echo $set1;


2.Loops might slow You

However, a loop is more supportive for a developer. it is usually a good support to reduce development effort. But the worse use of loop may become a thread of bad performance. Like given in below example.


<?php
// bad example
function wrngloop() 
{
sleep(10);
.return "Hello";

}

for ($i=0; $i<100; $i++) {
$value = wrngloop();
echo $value;
}

In this piece of example is variable is setup once for a cycle.


<?php

// better example
function wrngloop() {

sleep(10);
return "Hello";

}


$value = wrngloop();
for ($i=0; $i<100; $i++) {
echo $value;
}

Now you can find the problem.

To find performance issue look a while in code.


  • Detect big loops (for, for each, ...)
  • Do they iterate over a big amount of data?
  • check them.
  • is it possible to cache the operation inside the loop?
  • If yes, what are you waiting for?
  • If not, highlight them as potentially dangerous and focus your inspections on them. Small performance problems in your code can be multiplied.



3.Use Of Gearman

Gearman has a simple protocol and interface with an optimised, and threaded, a server written in C/C++ to minimise your application overhead.
it is an open source framework for application why not use it for

● Massive mailing systems

● PDF generation

● Image processing

● Logs


4.Improve DB Access

Another important tip is the usage of prepared statements. Why? The answer is simple. Let me show you one example:

Code A

$dbh = new PDO('pgsql:dbname=pg1;host=localhost', 'user', 'password');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$field1 = uniqid();
$dbh->beginTransaction();
foreach (range(1, 1000, 1) as $i) {
$stmt = $dbh->prepare("UPDATE test.tbl1 set field1='{$field1}' where id=1");
$field1 = $i;
$stmt->execute();
}
$dbh->commit();

Code B

$dbh = new PDO('pgsql:dbname=pg1;host=localhost', 'user', 'password');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$field1 = uniqid();
$dbh->beginTransaction();
$stmt = $dbh->prepare('UPDATE test.tbl1 set field1=:F1 where id=1');
foreach (range(1, 1000, 1) as $i) {
$field1 = $i;
$stmt->execute(array('F1' => $field1));
}
$dbh->commit();

The Code A sends the SQL update as one string and executes it 1,000 times.
The database will compile each update and execute. The Code B compiles it once and executes 1,000 times with different parameters.
There is another great benefit from using prepared statements, which is to prevent SQL injections.
But if you are talking about performance, you need to focus it.



Forte :



If you are looking to improve your Web performance, you should have an answer of these questions:


● How many database connections have I in my application?

● How much time does each select statement take?

● How many select statements are in code?

● Are we using these statements in loops?

● Do I really need these statements? Can I cache them at least with a TTL(Time to live)?

● Do we need to perform my transactions (Inserts, Updates drop) online inside the user request?

● why not queue them?

● How much CPU does the application use per request?

● How much memory does the application use per request?




Reference: piece of code is referred from http://stackoverflow.com/

Comments

Popular Posts