note 23922 deleted from function.require by dams

From: Date: Fri, 02 Aug 2002 10:47:57 +0000
Subject: note 23922 deleted from function.require by dams
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-34138@lists.php.net to get a copy of this message
When using a large number of require on the same file like when using a template that you have to use multiple times you might find it faster to read the file into a string and then evaluate it. In my example i am evaluating and requiring test2.php 1000 times. Eval was ALMOST always faster (10 - 100 times faster !). test2.php: <? echo "<td>$x</td>"; ?> test.php: <? function getmicrotime(){ list($usec, $sec) = explode(" ",microtime()); return ((float)$usec + (float)$sec); } $times = 1000; ?> Require test2.php <?=$times?> times.<br >Starting loop... <p> <table><tr> <? $time_start = getmicrotime(); for($x=1; $x<=$times; $x++) { require "test2.php"; } $time = getmicrotime() - $time_start; ?> </tr></table><br >Ending loop... <?=$time?> <br><br> Evaluate test2.php <?=$times?> times.<br >Starting loop... <p> <table><tr> <? $time_start = getmicrotime(); $filename = "test2.php"; $fd = fopen ($filename, "r"); $contents = fread ($fd, filesize ($filename)); fclose ($fd); for($x=1; $x<=$times; $x++) { eval("?>".$contents."<?"); } $time = getmicrotime() - $time_start; ?> </tr></table><br >Ending loop... <?=$time?> ---------- As you can see i evaluated the contents of test2.php starting with ?> and ending with <?. This way you can still run test2.php the normal way. If you think i am making a huge mistake doing this, please let me know.

« previous php.notes (#34138) next »