optimization

Improve PHP file loading

Sun, 11/15/2009 - 01:36quocbao

I think you have already known that require/include is faster than require_once/include_one. But sometime, we also need the functionality of require_once but do not suffer from the performance tradeoff. So here is a better way to re-invent PHP implement.

function requireOnce($file)
{
	static $dict=array();
 
	if (!isset($dict[$file]))
	{
		require $file;
		$dict[$file] = true;
	}
}

The idea is very simple: PHP array access is faster than require_once/include_one checking, so we just keep an array to store for loaded file. But the implement is not completed yet because we can have array key conflict here, requireOnce('path/to/file.php') and requireOnce('/absolute/path/to/file') will yeild an error. I shift the idea a bit to avoid the conflict: when we use require_once or include_once the file will always be a definition file, ie: a class or functions, and these files can be identify in other way by class name or a custom label. So I change the implement again

Categories: Programming Tags: optimization, php

The best way to access PHP global variable

Thu, 11/05/2009 - 13:20quocbao

I have been playing with PHP minor optimization for a while, but this is the first time I go for it seriously. My question for today is what's the best way to work with PHP global variables and how to achieve best performance for it. Rumours say that global keyword is slow, so this is a good time to check it out. And I also want to check if the number of variables affects the performance of the method.

Setup

The first thing todo is to define how we can access a global variable. The below is the list of ways to access global method that I use for the performance testing.

1. Use global keyword
It can't be simpler.

global $value;

2. Access $GLOBALS array
Instead of using the global keyword. I will access the global array directly.

$GLOBALS['value'];

3. Access $GLOBALS array with reference
Like the above method, but using reference. This way may be faster because we don't have to access the global array again and again.

$value =& $GLOBAL['value'];

4. Access $GLOBALS array and assign the value later
In this method, I fetch the value first, doing stuff with this and assign it to the global scope again. The code below illustrate the idea of this method.

$value = $GLOBAL['value'];
// doing stuff
$GLOBAL['value'] = $value;

5. Using a global array
Instead of using global variables in global scope. I store all global variables in an array and access it later. This method is useful because it reduces variable conflictment when the application grows.

global $ARR;
$ARR['value'];

6. Using a global object
Like the above method, but use an object to store the variable.

global $OBJ;
$obj->value;

7. Static class variable
I use a class to manage static variable. Many modern developers use this way to store global variable and configuration.

class GLB
{
	public static $vars;
}
 
GLB::$vars['value'];

8. Static function variable - reference return
This is a another way to store / retrive global variables. I want to compare it with other method to see how it's different to others.

function &globalzie($name)
{
	static $vars = array();
	return $vars[$name];
}
 
$value =& globalzie('value');

9. Static function variable - reference parameter
The same approach, but different way to retrive the variable. I don't like the reference assign operator on the above method.

function globalize($name,&$var)
{
	static $vars = array();
	$var =& $vars[$name];
}
 
globalzie('value',$value);

Syndicate content