# HMACSHA256 2.5 times faster than HMACSHA512

According to the answers to a previous question, the 512 version should be faster on x64 devices. And even if it were executed on an x86 (32 bit) device - there shouldn't be such a big difference.

Is there some explanation for this difference? (Or must it be an implementation issue.)

The following code returns almost 5 seconds for the HMACSHA512, and almost 2 seconds for HMACSHA256: (It's run on a Samsung J5 (2015) which has a Snapdragon 410 Processor with "CPU Bit Architecture 64-bit". Using Xamarin.Forms PCL, the Droid project.):

private void Button_Clicked(object sender, EventArgs e)
{
ICrypto crypto = DependencyService.Get<ICrypto>();
Stopwatch watch = new Stopwatch();

byte[] b = new byte[64];
watch.Start();
for (int i = 0; i < 10000; i++)
{
b = crypto.CalculateHmac512(b);
}
watch.Stop();
label.Text = watch.ElapsedMilliseconds.ToString();
}


And

class Crypto : ICrypto
{
//HMACSHA256 hmac = new HMACSHA256();
HMACSHA512 hmac = new HMACSHA512();

public byte[] CalculateHmac512(byte[] m)
{
return hmac.ComputeHash(m);
}
}


(On a 64 bit PC for 100,000 iterations I get around 200ms for 512, and around 130ms for 256. So I guess that Xamarin itself is 32 bit. But that still doesn't explain the x2.5 difference on Android.)

• Sha512 is only faster if you consider cost per byte, not cost per block. – CodesInChaos Jan 31 '18 at 19:52
• And don't forget that you need to perform an additional hash / block for HMAC as that contains two hash operations. So that alone could explain the difference rather well. – Maarten Bodewes Jan 31 '18 at 22:35

Also, speed of algorithms, especially crypto, depends a lot on the quality of their implementation and how well it is tailored to the hardware at hand. From where we stand, we can't even tell if HMACSHA512 or HMACSHA256 are native code, Just-In-Time-compiled, or interpreted. Benchmarking anything but native code is both rather pointless (if performance really matters, the critical section should be native) and harder (results tend to be unstable).