title: Why is a simple get-statement so slow?
body: <p>A few years back, I got an assignment at school, where I had to parallelize a Raytracer.<br />
  It was an easy assignment, and I really enjoyed working on it.</p>
  <p>Today, I felt like profiling the raytracer, to see if I could get it to run any faster (without completely overhauling the code). During the profiling, I noticed something interesting:</p>
  <pre><code>    // Sphere.Intersect
      public bool Intersect(Ray ray, Intersection hit)
      {
          double a = ray.Dir.x * ray.Dir.x +
                     ray.Dir.y * ray.Dir.y +
                     ray.Dir.z * ray.Dir.z;
          double b = 2 * (ray.Dir.x * (ray.Pos.x - Center.x) +
                          ray.Dir.y * (ray.Pos.y - Center.y) +
                          ray.Dir.z * (ray.Pos.z - Center.z));
          double c = (ray.Pos.x - Center.x) * (ray.Pos.x - Center.x) +
                     (ray.Pos.y - Center.y) * (ray.Pos.y - Center.y) +
                     (ray.Pos.z - Center.z) * (ray.Pos.z - Center.z) - Radius * Radius;
  
          // more stuff here
      }
  </code></pre>
  <p>According to the profiler, 25% of the CPU time was spent on <code>get_Dir</code> and <code>get_Pos</code>, which is why, I decided to optimize the code in the following way:</p>
  <pre><code>    // Sphere.Intersect
      public bool Intersect(Ray ray, Intersection hit)
      {
          Vector3d dir = ray.Dir, pos = ray.Pos;
          double xDir = dir.x, yDir = dir.y, zDir = dir.z,
                 xPos = pos.x, yPos = pos.y, zPos = pos.z,
                 xCen = Center.x, yCen = Center.y, zCen = Center.z;
  
          double a = xDir * xDir +
                     yDir * yDir +
                     zDir * zDir;
          double b = 2 * (xDir * (xPos - xCen) +
                          yDir * (yPos - yCen) +
                          zDir * (zPos - zCen));
          double c = (xPos - xCen) * (xPos - xCen) +
                     (yPos - yCen) * (yPos - yCen) +
                     (zPos - zCen) * (zPos - zCen) - Radius * Radius;
  
          // more stuff here
      }
  </code></pre>
  <p>With astonishing results.</p>
  <p>In the original code, running the raytracer with its default arguments (create a 1024x1024 image with only direct lightning and without AA) would take <strong>~88 seconds</strong>.<br />
  In the modified code, the same would take a little less than <strong>60 seconds</strong>.<br />
  I achieved a speedup of ~1.5 with only this little modification to the code.</p>
  <p>At first, I thought the getter for <code>Ray.Dir</code> and <code>Ray.Pos</code> were doing some stuff behind the scene, that would slow the program down.</p>
  <p>Here are the getters for both:</p>
  <pre><code>    public Vector3d Pos
      {
          get { return _pos; }
      }
  
      public Vector3d Dir
      {
          get { return _dir; }
      }
  </code></pre>
  <p>So, both return a Vector3D, and that's it.</p>
  <p>I really wonder, how calling the getter would take that much longer, than accessing the variable directly.</p>
  <p>Is it because of the CPU caching variables? Or maybe the overhead from calling these methods repeatedly added up? Or maybe the JIT handling the latter case better than the former? Or maybe there's something else I'm not seeing?</p>
  <p>Any insights would be greatly appreciated.</p>
  <h1>Edit:</h1>
  <p>As @MatthewWatson suggested, I used a <code>StopWatch</code> to time release builds outside of the debugger. In order to get rid of noise, I ran the tests multiple times. As a result, the former code takes <strong>~21 seconds</strong> (between 20.7 and 20.9) to finish, whereas the latter only <strong>~19 seconds</strong> (between 19 and 19.2).<br />
  The difference has become negligible, but it is still there.</p>