PHP Speed Improvement Tips


PHP is a popular web programming language that has been used in millions of website. Below are some simple tips of improving the PHP script, which can make the website a lot faster in some cases.

1. Use echo rather than print

echo is PHP inbuilt statement print is a function (similar to the printf in C). Using inbuilt functions certainly are fasters than calling external functions. The following code compares both.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  $a = 'a';
  $b = 'b';
  $c = 'c';
  define('MAXN', 20000);
  $s = microtime();
  for ($i = 0; $i < MAXN; $i ++)
  {
    echo $a.$b.$c;
  }
  $e1 = round(microtime() - $s, 9);
  $s = microtime();
  for ($i = 0; $i < MAXN; $i ++)
  {
    print $a.$b.$c;
  }  
  $e2 = round(microtime() - $s, 9);  
  echo $e1, ' and ', $e2;
  $a = 'a';
  $b = 'b';
  $c = 'c';
  define('MAXN', 20000);
  $s = microtime();
  for ($i = 0; $i < MAXN; $i ++)
  {
    echo $a.$b.$c;
  }
  $e1 = round(microtime() - $s, 9);
  $s = microtime();
  for ($i = 0; $i < MAXN; $i ++)
  {
    print $a.$b.$c;
  }  
  $e2 = round(microtime() - $s, 9);  
  echo $e1, ' and ', $e2;

The output 0.005623 and 0.006502′ indicates a slight improvement using echo.

2. Similarly using commas is better than periods if you simply want to print a list of values. The above script can be slightly modified to test the following two.

1
2
echo $a.$b.$c;
echo $a,$b,$c;
echo $a.$b.$c;
echo $a,$b,$c;

The reason is that using periods is treated as string concat operations while using commas simply print one by one.

3. Using require(), include() rather than require_once(), include_once(), if possible.

The *_once() functions will check if the included files have been included before. It is a very expensive operation. For most cases, these can be avoided. However, including the same files more than once may yield errors such as functions have been defined already. Sometimes, checking the file has been included before is necessary, but you should default to require() and include() in most cases.

4. Using single quote rather than double quote if possible.

The double quote will check if any variables are included in the string expression, which is simply time-consuming. Therefore, using echo ‘abc’ rather than echo “abc”

5. Use full path on require(), include(), require_once() and include_once() functions.

Including relative path may yield extra work in computing the full path. Therefore, include the full path if possible.

6. Avoid getters and setters for the class.

In PHP, there is no inline functions. Therefore, the getters and setters for class will incur extra work in calling these functions. Replace with the direct accesses to the class attributes. For example,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  class Test
  {
    private $a = 0;
    public $b = 0; 
    
    public GetA()
    {
      return $this->a;
    }
    
    public SetA($v)
    {
      $this->a = $v;
    }
  }
  
  $obj = new Test;
  $Test->SetA(1);
  echo $Test->GetA();
  $Test->b = 3;
  echo $Test->b;
  class Test
  {
    private $a = 0;
    public $b = 0; 
    
    public GetA()
    {
      return $this->a;
    }
    
    public SetA($v)
    {
      $this->a = $v;
    }
  }
  
  $obj = new Test;
  $Test->SetA(1);
  echo $Test->GetA();
  $Test->b = 3;
  echo $Test->b;

However, you have to make sure the attributes are public. This compromises the OOP (Object-Oriented Principle) but a significant amount of speed-ups may be observed if these are called very frequently.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
561 words
Last Post: Google Adsense Estimated Earnings
Next Post: PHP Cache Improvements

The Permanent URL is: PHP Speed Improvement Tips

2 Comments

  1. Tom Fillamin

Leave a Reply