Advertisement
Guest User

Performance: include() vs include_once()

a guest
Sep 30th, 2016
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.55 KB | None | 0 0
  1. --------------------------------------
  2. MAIN FILE (test_include.php)
  3. --------------------------------------
  4.  
  5. <?php
  6. $loop_time = 3;
  7. include('include_file1.php');
  8. include('include_file2.php');
  9. include('include_file3.php');
  10.  
  11. $iterations = 0;
  12. $start_time = microtime(true);
  13. $duration = 0;
  14. while ($duration <= $loop_time) {
  15.     include('include_file1.php');
  16.     $iterations++;
  17.     $duration = microtime(true) - $start_time;
  18. }
  19. echo 'include(large_file): ',$iterations,' iterations in ',$duration,' seconds',PHP_EOL;
  20.  
  21.  
  22. $iterations = 0;
  23. $start_time = microtime(true);
  24. $duration = 0;
  25. while ($duration <= $loop_time) {
  26.     include_once('include_file1.php');
  27.     $iterations++;
  28.     $duration = microtime(true) - $start_time;
  29. }
  30. echo 'include_once(large_file): ',$iterations,' iterations in ',$duration,' seconds',PHP_EOL;
  31.  
  32. echo PHP_EOL;
  33.  
  34. $iterations = 0;
  35. $start_time = microtime(true);
  36. $duration = 0;
  37. while ($duration <= $loop_time) {
  38.     include('include_file2.php');
  39.     $iterations++;
  40.     $duration = microtime(true) - $start_time;
  41. }
  42. echo 'include(small_file): ',$iterations,' iterations in ',$duration,' seconds',PHP_EOL;
  43.  
  44.  
  45. $iterations = 0;
  46. $start_time = microtime(true);
  47. $duration = 0;
  48. while ($duration <= $loop_time) {
  49.     include_once('include_file2.php');
  50.     $iterations++;
  51.     $duration = microtime(true) - $start_time;
  52. }
  53. echo 'include_once(small_file): ',$iterations,' iterations in ',$duration,' seconds',PHP_EOL;
  54.  
  55. echo PHP_EOL;
  56.  
  57. $iterations = 0;
  58. $start_time = microtime(true);
  59. $duration = 0;
  60. while ($duration <= $loop_time) {
  61.     include('include_file3.php');
  62.     $iterations++;
  63.     $duration = microtime(true) - $start_time;
  64. }
  65. echo 'include(empty_file): ',$iterations,' iterations in ',$duration,' seconds',PHP_EOL;
  66.  
  67.  
  68. $iterations = 0;
  69. $start_time = microtime(true);
  70. $duration = 0;
  71. while ($duration <= $loop_time) {
  72.     include_once('include_file3.php');
  73.     $iterations++;
  74.     $duration = microtime(true) - $start_time;
  75. }
  76. echo 'include_once(empty_file): ',$iterations,' iterations in ',$duration,' seconds',PHP_EOL;
  77.  
  78.  
  79. /* REVERSE */
  80. echo PHP_EOL,'reversing order and retesting ...',PHP_EOL,PHP_EOL;
  81. /***********/
  82.  
  83. $iterations = 0;
  84. $start_time = microtime(true);
  85. $duration = 0;
  86. while ($duration <= $loop_time) {
  87.     include_once('include_file3.php');
  88.     $iterations++;
  89.     $duration = microtime(true) - $start_time;
  90. }
  91. echo 'include_once(empty_file): ',$iterations,' iterations in ',$duration,' seconds',PHP_EOL;
  92.  
  93.  
  94. $iterations = 0;
  95. $start_time = microtime(true);
  96. $duration = 0;
  97. while ($duration <= $loop_time) {
  98.     include('include_file3.php');
  99.     $iterations++;
  100.     $duration = microtime(true) - $start_time;
  101. }
  102. echo 'include(empty_file): ',$iterations,' iterations in ',$duration,' seconds',PHP_EOL;
  103.  
  104. echo PHP_EOL;
  105.  
  106. $iterations = 0;
  107. $start_time = microtime(true);
  108. $duration = 0;
  109. while ($duration <= $loop_time) {
  110.     include_once('include_file2.php');
  111.     $iterations++;
  112.     $duration = microtime(true) - $start_time;
  113. }
  114. echo 'include_once(small_file): ',$iterations,' iterations in ',$duration,' seconds',PHP_EOL;
  115.  
  116.  
  117. $iterations = 0;
  118. $start_time = microtime(true);
  119. $duration = 0;
  120. while ($duration <= $loop_time) {
  121.     include('include_file2.php');
  122.     $iterations++;
  123.     $duration = microtime(true) - $start_time;
  124. }
  125. echo 'include(small_file): ',$iterations,' iterations in ',$duration,' seconds',PHP_EOL;
  126.  
  127. echo PHP_EOL;
  128.  
  129. $iterations = 0;
  130. $start_time = microtime(true);
  131. $duration = 0;
  132. while ($duration <= $loop_time) {
  133.     include_once('include_file1.php');
  134.     $iterations++;
  135.     $duration = microtime(true) - $start_time;
  136. }
  137. echo 'include_once(large_file): ',$iterations,' iterations in ',$duration,' seconds',PHP_EOL;
  138.  
  139.  
  140. $iterations = 0;
  141. $start_time = microtime(true);
  142. $duration = 0;
  143. while ($duration <= $loop_time) {
  144.     include('include_file1.php');
  145.     $iterations++;
  146.     $duration = microtime(true) - $start_time;
  147. }
  148. echo 'include(large_file): ',$iterations,' iterations in ',$duration,' seconds',PHP_EOL;
  149.  
  150. ?>
  151.  
  152. ---------------------------------
  153. LARGE INCLUDE (include_file1.php)
  154. ---------------------------------
  155. <?php
  156. if (defined('INCLUDE_FILE2')) return;
  157.  
  158. define('INCLUDE_FILE2',true);
  159. sleep ($loop_time);
  160.  
  161. /* COPY / PASTE / REPEAT the following line about 1250 times to increase file size */
  162. /* Use up a bunch of memory or disk space to make the include more "costly" */
  163. /* Use up a bunch of memory or disk space to make the include more "costly" */
  164. /* ... /*
  165. ?>
  166.  
  167. ---------------------------------
  168. SMALL INCLUDE (include_file2.php)
  169. ---------------------------------
  170. <?php
  171. if (defined('INCLUDE_FILE2')) return;
  172.  
  173. define('INCLUDE_FILE2',true);
  174. sleep ($loop_time);
  175. ?>
  176.  
  177. ----------------------------------
  178. EMPTY INCLUDE (include_file3.php)
  179. ----------------------------------
  180.  
  181.  
  182. ----------------------------------
  183. TEST RESULTS
  184. ----------------------------------
  185. D:\www\manual>php test_include.php
  186. include(large_file): 12118 iterations in 3.015625 seconds
  187. include_once(large_file): 196366 iterations in 3.015625 seconds
  188.  
  189. include(small_file): 34986 iterations in 3.015625 seconds
  190. include_once(small_file): 222284 iterations in 3.015625 seconds
  191.  
  192. include(empty_file): 68155 iterations in 3.015625 seconds
  193. include_once(empty_file): 220433 iterations in 3.015625 seconds
  194.  
  195. reversing order and retesting ...
  196.  
  197. include_once(empty_file): 192640 iterations in 3.015625 seconds
  198. include(empty_file): 68381 iterations in 3.015625 seconds
  199.  
  200. include_once(small_file): 222204 iterations in 3.015625 seconds
  201. include(small_file): 34714 iterations in 3.015625 seconds
  202.  
  203. include_once(large_file): 226388 iterations in 3.015625 seconds
  204. include(large_file): 9577 iterations in 3.015625 seconds
  205.  
  206. D:\www\manual>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement