2011-12-24

Resource Performance



You ever run your application and realize it's taking a long time to process?

It would probably be a good idea to take a look at your application and see how many times
you open and close a resource or a stream. If you have a lot of functions that open a stream,
query it, and then close the stream, then it's likely that might be the bottleneck of the
application.



In order to reduce the amount of time it takes to process the application, it's usually best to
open the steams to the resources needed when the application starts, and then close them when the
application ends. Here's an example class that deals with files:



class FileHandler
{

/**
* Implements the singleton pattern.
* @var FileHandler
*/
protected static $_instance;

/**
* Holds the current file pointer.
* @var [resource] File pointer
*/
protected $_fp;

/**
* Starts up the resource handler.
* @param filename string The path to a file to open.
* @return void
*/
public function __construct($filename = null)
{
if ($filename !== null) {
$this->openFile($filename);
}
}

/**
* Closes the file handler and destroys this object
* @return void
*/
public function __destruct()
{
$this->closeFile();
}

/**
* Opens a new file resource.
* @param filename string The name of the file to open.
* @return void
*/
public function openFile($filename)
{
if (!file_exists($filename)) {
throw new Exception("Could not open file: $filename");
}
if (!is_readable($filename)) {
throw new Exception("Could not read file: $filename");
}

// Open a file for reading and writing, place the file
// pointer at the beginning of the file.
$this->_fp = fOpen($filename, 'r+');
}

/**
* Gets the file handler
* @return [resource] File pointer
*/
public function getHandle()
{
return $this->_fp;
}

/**
* Closes the file stream
* @return void
*/
public function closeFile()
{
fClose($this->_fp);
}
}


Note how this object creates a pointer to a file on instantiation and
closes the file pointer when the class is destroyed. That means, if
you create an instance of this class and use it throughout your application,
as opposed to creating functions that simply open a resource, query the
file and then close the resource, you'll be surprised to find the impact
in performance it can have on such a small operation. Multiply that by
the number of users that could possibly be accessing your application,
and the savings you'll make in resources will be phenominal!



Its important to structure your application so that you open a resource
as few times as possible. This doesn't just apply to files, it applies
to all streams; this can include database connections, network streams,
and anything that involves reading and writing. In addition making calls
to either read or write to the stream is also a costly operation. It's
best to gather as much data as possible in the stream, and that means
obtaining all the information you will need in the operation before
you process the information in the stream. Once you've obtained the
necessary information, you should then be able to process the data without
too much of an issue. Follow up with a final write of the data to the
stream to save the data, if necessary, and then close the stream as you
exit the application.



Following these simple steps can lead to a more efficient and faster
application.


No comments:

Post a Comment