note 23922 added to function.require

From: Date: Thu, 01 Aug 2002 14:06:47 +0000
Subject: note 23922 added to function.require
Groups: php.notes 
Request: Send a blank email to php-notes+get-34088@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. -- http://www.php.net/manual/en/function.require.php http://master.php.net/manage/user-notes.php?action=edit+23922 http://master.php.net/manage/user-notes.php?action=delete+23922 http://master.php.net/manage/user-notes.php?action=reject+23922

« previous php.notes (#34088) next »