title: Accessing Big Arrays in PHP
body: <p>I've been doing some profiling on different methods of accessing large(ish) arrays of data in PHP.  The use case is pretty simple: some of our tools output data into PHP files as associative arrays and these files are considered static data by the application.  We make games, so some examples of data files would include items in a catalog, tasks that a user must complete, or definitions for maps:</p>
  
  <pre><code>&lt;?php
  $some_data = array(
      ...lots and lots of stuff in here...
  );
  ?&gt;
  </code></pre>
  
  <p>Since these arrays are large-ish(400K), and much of our code is interested in this data, it becomes necessary to access this data as efficiently as possible.  I settled on timing 3 different patterns for doing this.  After presenting the methods I will share my results below.</p>
  
  <p>What I'm looking for is some experience based validation on these methods and their timing as well as any other methods to try out.</p>
  
  <h2>Method #1: getter function</h2>
  
  <p>In the method, the exporter actually creates a file that looks like:</p>
  
  <pre><code>&lt;?php
  function getSomeData()
  {
      $some_data = array(
          ...lots and lots of stuff here...
      );
      return $some_data;
  }
  ?&gt;
  </code></pre>
  
  <p>Client code can then get the data by simply calling getSomeData() when they want it.  </p>
  
  <h2>Method #2: global + include</h2>
  
  <p>In this method the data file looks identical to the original code block above, however the client code must jump through a few hoops to get the data into a local scope.  This assumes the array is in a file called 'some_data.php';</p>
  
  <pre><code>global $some_data; //must be the same name as the variable in the data file...
  include 'some_data.php';
  </code></pre>
  
  <p>This will bring the $some_data array into scope, though it is a bit cumbersome for client code (my opinion).</p>
  
  <h2>Method #3: getter by reference</h2>
  
  <p>This method is nearly identical to Method #1, however the getter function does not return a value but rather sets a reference to the data.</p>
  
  <pre><code>&lt;?php
  function getSomeDataByRef($some_data)
  {
      $some_data = array(
          ...lots and lots of stuff here...
      );
      return $some_data;
  }
  ?&gt;
  </code></pre>
  
  <p>Client code then retrieves the data by declaring a local variable (called anything) and passing it by reference to the getter:</p>
  
  <pre><code>$some_data_anyname = array();
  getSomeDataByRef(&amp;$some_data_anyname);
  </code></pre>
  
  <h2>Results</h2>
  
  <p>So I ran a little script that runs each of these methods of retrieving data 1000 times on and averages the run time (computed by microtime(true) at the beginning and end).  The following are my results (in ms, running on a MacBookPro 2GHz, 8GB RAM, PHP version 5.3.4):</p>
  
  <h3>METHOD #1:</h3>
  
  <blockquote>
    <p>AVG: 0.0031637034416199
    MAX: 0.0043289661407471
    MIN: 0.0025908946990967</p>
  </blockquote>
  
  <h3>METHOD #2:</h3>
  
  <blockquote>
    <p>AVG: 0.01434082698822
    MAX: 0.018275022506714
    MIN: 0.012722969055176</p>
  </blockquote>
  
  <h3>METHOD #3:</h3>
  
  <blockquote>
    <p>AVG: 0.00335768699646
    MAX: 0.0043489933013916
    MIN: 0.0029017925262451</p>
  </blockquote>
  
  <p>It seems pretty clear, from this data anyway, that the global+include method is inferior to the other two, which are "negligible" difference.</p>
  
  <p>Thoughts? 
  Am I completely missing anything? (probably...)</p>
  
  <p>Thanks in advance!</p>