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!
blog comments powered by Disqus |
|
|
|
|
|
|
|