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

Reading data from a flat text file - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- 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: