title: Recursive method 10x slower than interative
body: <p>The code is cleaned as far as i could to show my problem.
  Basically its an octree search+intersect.
  the intersect function comes from an SDK (the whole project is a plugin for rhino).
  If i make a loop with intersection calls, its 10 times faster than the recursive search through the octree. Stranger even - i isolated the timing of the intersect call - and inside the recursive it was 8 times slower than in a loop.
  There could be probably 1000 reasons why it behaves like this, but i hope i made some obvious mistake someone can spot by looking over the code.</p>
  
  <p>there is the slow recusive piece:</p>
  
  <pre><code>public void NewRayCast()
  {            
      int runs = 500000;                          //how many rays we cast
      Point3d raypos = new Point3d(0, 0, 0);      //raystart
      Ray3d ray = new Ray3d();                
  
      Random r = new Random();                    //here we create targets to scatter the ray directions
      Vector3d[] targets = new Vector3d[runs];
      for (int i = 0; i &lt; runs; i++)
      {
          targets[i] = new Vector3d(r.NextDouble() * 200 - 100, 500, r.NextDouble() * 200 - 100);
      }
  
      for (int i = 0; i &lt; runs; i++)
      {
          boxes = new List&lt;int&gt;();            // this collects the octree leaves the rays hits
          rayline.From = ray.Position;
          rayline.To = ray.Position + ray.Direction;                
          LineBoxer(starter);                 // this starts the raycasting - starter is a array with 1 value (the scene bounding box)
      }            
  }
  
  public void LineBoxer(int[] check)    // Cast a ray against boxes
  {            
      for (int i = 0; i &lt; check.Length; i++)  // check only because the first call is with the scene box (1 index)
      {
          if (octmanB.node[check[i]].closed == false) // if node is a dead end &gt; empty we skip it
          {                                       
              isect = Rhino.Geometry.Intersect.Intersection.LineBox(rayline, octmanB.node[check[i]].bbox, 0, out ival); // intersection function, changing to an arbitrary bounding box doesnt speed it up either                    
              if (isect == true)
              {                        
                  if (octmanB.node[check[i]].leaf == false)   // continue with recursion
                  {
                      LineBoxer(octmanB.node[check[i]].child);
                  }
                  else
                  {
                      boxes.Add(check[i]);    // here we have a leaf
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>and here the fast one:</p>
  
  <pre><code>public void FasterTestRun()
  {
      int runs = 500000;                       
      Line line = new Line(new Point3d(1, 1, 1), new Point3d(0, 1000, 0));
      BoundingBox box = new BoundingBox(new Point3d(-50, 50, -50), new Point3d(50, 150, 50));
  
      bool isect;
      Interval v = new Interval();
  
      Random r = new Random();
      Point3d[] targets = new Point3d[runs];
      for (int i = 0; i &lt; runs; i++)
      {
          targets[i] = new Point3d(r.NextDouble() * 20 - 10, 1000, r.NextDouble() * 20 - 10);
      }
  
      for (int i = 0; i &lt; runs; i++)
      {
          line.To = targets[i];                
          isect = Rhino.Geometry.Intersect.Intersection.LineBox(line, box, 0, out v);                
      }            
  }
  </code></pre>
  
  <p>thanks!</p>