title: DateTime.DayOfWeek micro optimization
body: <p><strong>First of all:</strong></p>
  <ol>
  <li><p>I'm asking this question just for fun and eager to learn. I have to admit I love to mess around with micro-optimizations (Although they have never led to any significant increase in speed in any of my developments).</p>
  </li>
  <li><p>The <code>DateTime.DayOfWeek</code> method does not represent a bottleneck in any application of mine.</p>
  </li>
  <li><p>And it is <em>highly unlikely</em>  to be a problem in any other. If anyone is thinking that this method has an impact on the performance of his application,
  he should think about <a href="http://en.wikipedia.org/wiki/Program_optimization#When_to_optimize" rel="nofollow noreferrer">When to optimize</a> and then, he should perform a profiling.</p>
  </li>
  </ol>
  <p>Decompiling <code>DateTime</code> class with ILSpy, we find out how <code>DateTime.DayOfWeek</code> is implemented:</p>
  <pre><code>public DayOfWeek DayOfWeek
  {
      [__DynamicallyInvokable, TargetedPatchingOptOut(&quot;Performance critical to inline across NGen image boundaries&quot;)]
      get
      {
          return (DayOfWeek)((this.InternalTicks / 864000000000L + 1L) % 7L);
      }
  }
  
  public long Ticks
  {
      [__DynamicallyInvokable, TargetedPatchingOptOut(&quot;Performance critical to inline this type of method across NGen image boundaries&quot;)]
      get
      {
          return this.InternalTicks;
      }
  }
  </code></pre>
  <p>This method performs the following:</p>
  <ol>
  <li><p>The ticks corresponding to the current day are divided by the existing number of ticks in a day.</p>
  </li>
  <li><p>We add 1 to the foregoing result, in order that the remainder of division of 7 is between the numbers 0 and 6.</p>
  </li>
  </ol>
  <p>Is this the only way to calculate the day of the week?</p>
  <p>Would it be possible to reimplement this in order to make it run faster?</p>