SunQuest
 
       PHP
  Home arrow PHP arrow Page 4 - Comparing Files and Databases with PHP...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
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? 
PHP

Comparing Files and Databases with PHP Benchmarking Applications
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Over the course of this last episode of the series, you’ll learn how to use PHP...
     

       

    PHP ARTICLES

    - Viewing and Editing Tasks for a Project Mana...
    - More on Private Methods with PHP 5 Member Vi...
    - Adding Tasks to a Project Management Applica...
    - Utilizing Private Methods with PHP 5 and Mem...
    - Making Changes in a Project Management Appli...
    - Defining Public and Protected Methods with M...
    - HTML for a Project Management Application
    - Using Subclasses and Accessors with Member V...
    - Implementing Internet Protocols with PHP
    - Project Management: The Application
    - Working with Private Properties to Protect P...
    - Protecting PHP 5 Class Data with Member Visi...
    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway