PHP
  Home arrow PHP arrow Page 4 - Comparing Files and Databases with PHP Benchmarking Applications
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Comparing Files and Databases with PHP Benchmarking Applications
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2008-05-07


    Table of Contents:
  • Comparing Files and Databases with PHP Benchmarking Applications
  • Reading data from a sample database table
  • Reading data from a flat text file
  • Reading and writing compressed file data

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Comparing Files and Databases with PHP Benchmarking Applications - Reading and writing compressed file data
    ( Page 4 of 4 )

    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){

    if(!file_exists($dataFile)){

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

    }

    $this->dataFile=$dataFile;

    }

    // read data from file

    public function readFile(){

    if(!$contents=file_get_contents($this->dataFile)){

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

    }

    return $contents;

    }

    // write compressed data to file

    public function writeFile(){

    $data='';

    if(!$fp=fopen($this->dataFile,'a+')){

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

    }

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

    $data.=$line."n";

    }

    fwrite($fp,gzencode($data,9));

    fclose($fp);

    }

    // add data to $data array

    public function addData($data){

    if(!is_string($data)){

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

    }

    $this->data[]=$data;

    }

    }


    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)

    try{

    // instantiate 'Timer' object

    $timer=new Timer();

    // instantiate 'FileProcessor' object

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

    // start timer

    $timer->start();

    // read compressed data from file

    $fileProc->readFile();

    // stop timer

    $elapsedTime=$timer->stop();

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

    }

    catch(Exception $e){

    echo $e->getMessage();

    exit();

    }


    /*

    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
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek