Home arrow PHP arrow Page 4 - Comparing Files and Databases with PHP Benchmarking Applications

Reading and writing compressed file data - PHP

If you’re a web developer who wants to learn how to create simple benchmarking scripts with PHP, this set of comprehensive articles will offer you a friendly introduction to the subject. Welcome to the final part of the series “Benchmarking Applications with PHP.” Made up of three tutorials, this series teaches you, via copious hands-on examples, how to implement some simple approaches to determine the performance of different PHP applications.

  1. Comparing Files and Databases with PHP Benchmarking Applications
  2. Reading data from a sample database table
  3. Reading data from a flat text file
  4. Reading and writing compressed file data
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
May 07, 2008

print this article



To finish this trip, where the performance of different scripts is roughly evaluated via a "Timer" class, the final example that I plan to create here will be based on defining a brand new file processor class. This class will be capable of reading and writing compressed data by utilizing a text file.

Basically, the definition of this file processing class looks like this:

class FileProcessor{

private $data=array();

private $dataFile;

public function __construct($dataFile){


throw new Exception('Invalid data file!');




// read data from file

public function readFile(){


throw new Exception('Error reading data file!');


return $contents;


// write compressed data to file

public function writeFile(){



throw new Exception('Error opening data file!');


foreach($this->data as $line){






// add data to $data array

public function addData($data){


throw new Exception('Data must be a string!');





As shown above, the "FileProcessor" class looks nearly identical to the one used with previous examples. However, its main difference rests on the fact that it compresses the corresponding input data before saving it to a file. This should eventually decrease the time required for reading the content.

Well, in this case, I used the term "eventually" because reality tells a different story. There's no significant improvement if you compare this example with the previous one, which works with uncompressed data.

To demonstrate this concept, take a look at the following benchmarking script:

// example using 'FileProcessor' class (reads compressed data from file)


// instantiate 'Timer' object

$timer=new Timer();

// instantiate 'FileProcessor' object

$fileProc=new FileProcessor('data_file.txt');

// start timer


// read compressed data from file


// stop timer


echo 'reading compressed data to file took '. $elapsedTime.' seconds';


catch(Exception $e){

echo $e->getMessage();




displays the following:

Reading compressed data from file took 0.00063 seconds


In this specific case, reading compressed data from the corresponding data file doesn't introduce a significant performance improvement (we are taking into account that this script has been tested only locally).

Nevertheless, if a similar file processing application is tested remotely in real conditions, working with compressed data should result in better performance, depending strongly on different testing environments. Naturally, there's plenty of room to experiment here. Thus, I suggest you use a benchmarking script for testing your applications in real world conditions.

Final thoughts

That's all for now. In this three-part series, you hopefully learned how to create simple benchmarking scripts that use the "microtime()" built-in PHP function as the main foundation for developing timing functions and classes.

Obviously, evaluating the performance of a specific PHP application depends on many factors, and in this case, I only showed you a few illustrative examples of how to use the remarkable timing capabilities provided natively by PHP.

See you in the next PHP tutorial!

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: