PHP
  Home arrow PHP arrow Page 3 - 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? 
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 data from a flat text file
    ( Page 3 of 4 )

    As I said in the section that you just read, the next benchmarking example that I plan to create here relies on fetching the ten rows that you saw before using a plain text file. First off, I need to define a class that fetches the records from the text file. Therefore, below I listed the signature for this class. Here it is:


    // define 'FileProcessor' class

    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 nl2br($contents);

    }

    // write data to file

    public function writeFile(){

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

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

    }

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

    if(!fwrite($fp,$line."n")){

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

    }

    }

    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;

    }

    }

    Indeed, the signature of the above "FileProcessor" class is really easy to follow. In short, all that this class does is read and write data using a flat text file for storing purposes. Of course, most of its methods can be significantly improved, but for now, it's more than enough for setting up this benchmarking example.

    Okay, having showed you how the previous file processor class looks, let me show you a couple of short scripts. The first one inserts ten new records into a sample text file, and the second one reads this data and prints the contents on the browser.

    That being said, here is the listing for the corresponding examples:


    // example using 'FileProcessor' class (writes data to file)

    try{

    // instantiate 'Timer' class

    $timer=new Timer();

    // instantiate 'FileProcessor' object

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

    // start timer

    $timer->start();

    $fileProc->addData('ID: 1 Name: user1 Email: user1@domain.com');

    $fileProc->addData('ID: 2 Name: user2 Email: user2@domain.com');

    $fileProc->addData('ID: 3 Name: user3 Email: user3@domain.com');

    $fileProc->addData('ID: 4 Name: user4 Email: user4@domain.com');

    $fileProc->addData('ID: 5 Name: user5 Email: user5@domain.com');

    $fileProc->addData('ID: 6 Name: user6 Email: user6@domain.com');

    $fileProc->addData('ID: 7 Name: user7 Email: user7@domain.com');

    $fileProc->addData('ID: 8 Name: user8 Email: user8@domain.com');

    $fileProc->addData('ID: 9 Name: user9 Email: user9@domain.com');

    $fileProc->addData('ID: 10 Name: user10 Email: user10@domain.com');

    // write data to file

    $fileProc->writeFile();

    // stop timer

    $elapsedTime=$timer->stop();

    echo 'Writing data to file took '.$elapsedTime.' seconds';

     

    /*

    displays the following

    Writing data to file took 0.00124 seconds

    */

     

    }

    catch(Exception $e){

    echo $e->getMessage();

    exit();

    }

     

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

    try{

    // instantiate 'Timer' object

    $timer=new Timer();

    // instantiate 'FileProcessor' object

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

    // start timer

    $timer->start();

    // read data from file

    $data=$fileProc->readFile();

    // stop timer

    $elapsedTime=$timer->stop();

    echo 'File data is listed below: <br />'.$data;

    echo 'Reading data from file took '.$elapsedTime.' seconds';

    }

    catch(Exception $e){

    echo $e->getMessage();

    exit();

    }


    /* displays the following:


    File data is listed below:

    ID: 1 Name: user1 Email: user1@domain.com

    ID: 2 Name: user2 Email: user2@domain.com

    ID: 3 Name: user3 Email: user3@domain.com

    ID: 4 Name: user4 Email: user4@domain.com

    ID: 5 Name: user5 Email: user5@domain.com

    ID: 6 Name: user6 Email: user6@domain.com

    ID: 7 Name: user7 Email: user7@domain.com

    ID: 8 Name: user8 Email: user8@domain.com

    ID: 9 Name: user9 Email: user9@domain.com

    ID: 10 Name: user10 Email: user10@domain.com

    Reading data from file took 0.00061 seconds

    */


    As you can see, the first example populates the sample "data_file.text" file with exactly ten records and finalizes its execution by displaying the amount of time this process took. However, I'd like you to pay attention to the second case, where things are definitely more interesting. In this particular example, the reverse process is performed -- the data is retrieved from the mentioned text file and echoed on screen.

    If you compare the benchmarking values displayed when fetching the rows from the previous database table and when retrieving the data from the respective text file, then you'll realize that this last case is significantly faster.

    Even though all the examples were tested on a local server, the "Timer" class that was used in all cases was very useful for evaluating the performances of different PHP scripts. Didn't I tell you that benchmarking scripts with PHP was easy?

    Okay, at this point, you hopefully understand how to create simple timing scripts with PHP. However, we've not come to the end of this journey yet. In the final section of this article, I'll show you a concluding example that will evaluate the performance of another sample class that will read and write data by way of a text file too.

    To see how this final example will be developed, click on the link and keep reading.



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

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT