hello,
in my script, I have to include many files, and it will often happen that a needed file was already included
My doubt is: is it more efficient to use an array of already included filenames and call include only if the name is not in_array(), or I should just use include_once every time? This is executed lots of times, so even a slight performance difference matters a lot.
I couldn't find info about include_once functioning, and I wonder if it maintains an internal array of included files (and if it does, what's the complexity of the structure of such array) or accesses the file system every time. In that last case, maintaining an array myself would be much faster.
What do you think?

Thanks!

    A list of included files is maintained internally (hitting the file system every time wouldn't provide anything useful about whether a file has already been included or not). The list can be retrieved as an array using [man]get_included_files[/man].

    On the subject of whether it's more efficient or not: first, it's generally more efficient to use existing inbuilt mechanisms than reimplement duplicates from scratch (for one thing, the inbuilt mechanism is run as native machine code instead of parsed/compiled/interpreted PHP/bytecode). Second, your code is almost certainly not so time critical that you need to worry about the difference in performance, only the simplicity of the code - so which is simpler?

      4 days later

      I tried this code:

      <?php
      
      $numtests = 1000;
      global $arr;
      $arr = array();
      
      function myrequire($filename) {
      	global $arr;
      	if(in_array($filename,$arr))
      		return;
      	require($filename);
      	array_push($arr,$filename);
      }
      
      
      $start = microtime(true);
      for($i=1; $i<=$numtests; $i++) {
      	require_once('inc.php');
      	require_once('inc1.php');
      	require_once('inc2.php');
      	require_once('inc3.php');
      	require_once('inc4.php');
      	require_once('inc5.php');
      	require_once('inc6.php');
      	require_once('inc7.php');
      	require_once('inc8.php');
      	require_once('inc9.php');
      }
      $end = microtime(true);
      $ex = $end - $start;
      echo "\n\n with require_once $ex \n\n";
      
      
      $start = microtime(true);
      for($i=1; $i<=$numtests; $i++) {
      	myrequire('inc.php');
      	myrequire('inc1.php');
      	myrequire('inc2.php');
      	myrequire('inc3.php');
      	myrequire('inc4.php');
      	myrequire('inc5.php');
      	myrequire('inc6.php');
      	myrequire('inc7.php');
      	myrequire('inc8.php');
      	myrequire('inc9.php');
      }
      $end = microtime(true);
      $ex = $end - $start;
      echo "\n\n with myrequire $ex \n\n";
      
      ?>
      

      ...and the winner is by far the homemade version:

       with require_once 0.179884910583 
      
       with myrequire 0.0404860973358 
      

      at least on my pc. Am I missing anything?

        Write a Reply...