title: Performance testing two methods of PowerShell integer division
body: <p>Microsoft technet <a href="http://technet.microsoft.com/en-us/library/ee176879.aspx" rel="nofollow">suggests <code>[Math]::Floor([int]$a / [int]$b)</code> for integer division</a>. I believe that <code>[int][Math]::Floor($a / $b)</code> is both more readable as well as more performant due to one less cast operation. <a href="https://gist.github.com/1586729" rel="nofollow">I have proven both methods equivalent.</a> However, I cannot get consistent results. My methodology involves repeating both methodologies 10,000 times and measuring the results with the <a href="http://technet.microsoft.com/en-us/library/dd347702.aspx" rel="nofollow">Measure-Command</a> cmdlet. However cannot constuct a test where one test performs better than another test repeatedly. My code is below:</p>
  
  <pre><code>Write-Host
  $loopLength = 10000
  
  $runtime = Measure-Command {
      1..$loopLength | ForEach-Object {
          Foreach ($divisor in 2,3,5,7) {
              [Math]::Floor([int]$_ / [int]$divisor) &gt; $null
          }
      }
  }
  
  "Double Cast: $($runtime.TotalMilliSeconds)"
  
  $runtime = Measure-Command {
      1..$loopLength | ForEach-Object {
          Foreach ($divisor in 2,3,5,7) {
              [int][Math]::Floor($_ / $divisor) &gt; $null           
          }
      }
  }
  "Single Cast: $($runtime.TotalMilliSeconds)"
  </code></pre>
  
  <p>How do I modify my code so I get consistent results that prove one method is better than another.</p>