While working on a presentation about iterative hashing techniques (hashing a hash n times to make it harder to break) I decided to do a few simple performance tests to see what the time tradeoff would be for more iterations. The results though confused me. When running a thousand iterations it took an average of 0.36 seconds but when I upped the iterations to 10,000 the process took less time (0.19 seconds average). I am not 100% sure why this is happening.
| Iterations | Time (seconds) |
| 1,000 | 0.36 |
| 10,000 | 0.19 |
| 100,000 | 1.32 |
Here is the code I am using right now:
protected void Button1_Click(object sender, EventArgs e)
{
this.lblInput.Text += DoIterations(1000);
this.lblInput.Text += DoIterations(1000);
this.lblInput.Text += DoIterations(10000);
this.lblInput.Text += DoIterations(10000);
this.lblInput.Text += DoIterations(100000);
this.lblInput.Text += DoIterations(100000);
}
private string DoIterations(int count)
{
DateTime start = DateTime.Now;
byte[] bytes = System.Text.Encoding.ASCII.GetBytes("ThisIsMyPassword");
for (int i = 0; i < count; i++)
{
bytes = ComputeHash(bytes);
}
TimeSpan span = DateTime.Now.Subtract(start);
return count + " : " + span.Seconds + "." + span.Milliseconds + "<br />";
}
private byte[] ComputeHash(byte[] input)
{
SHA512Managed sha = new SHA512Managed();
return sha.ComputeHash(input);
}
Anyone have any ideas on why this might be happening?