C#のハッシュ関数の処理速度を計測

C#でのハッシュ関数の処理速度を、やっつけプログラムを書いて測ってみた。
対象とするハッシュ関数は、

の5つ。
1024バイトのランダムデータを、各ハッシュ関数を用いて40万回計算し、その所要時間を計測した。
結果は次の通り。

Hash Algorithm calculation test, EACHSIZE=1024, 400000 times calculation.
Creating random data...
done.

Calculating md5 Hash for random data...
 Calculate time: 4109.375ms, 0.0102734375ms per calculation
done.

Calculating sha1 Hash for random data...
 Calculate time: 4453.125ms, 0.0111328125ms per calculation
done.

Calculating sha1managed Hash for random data...
 Calculate time: 4812.5ms, 0.01203125ms per calculation
done.

Calculating sha256managed Hash for random data...
 Calculate time: 9578.125ms, 0.0239453125ms per calculation
done.

Calculating sha384managed Hash for random data...
 Calculate time: 16687.5ms, 0.04171875ms per calculation
done.

Calculating sha512managed Hash for random data...
 Calculate time: 16421.875ms, 0.0410546875ms per calculation
done.

test finished.
ハッシュ関数 1回あたりの所要時間[ms]
MD5 0.0103
SHA1 0.0111
SHA-256 0.0239
SHA-384 0.0417
SHA-512 0.0411

SHA1MD5の差が意外と小さい。コリジョンのことを考えると、SHA1が良いかも。

計測に使用したソース

using System;
using System.Collections.Generic;
using System.Text;

namespace hashtest
{
    class Program
    {
        static void Main(string[] args)
        {
            const int EACHSIZE = 1024;
            const int MAXTRY = 400000;

            // 使用する乱数生成系
            System.Random rnd = new Random();

            // 使用するハッシュプロバイダ
            System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            System.Security.Cryptography.SHA1CryptoServiceProvider sha1 = new System.Security.Cryptography.SHA1CryptoServiceProvider();
            System.Security.Cryptography.SHA1Managed sha1managed = new System.Security.Cryptography.SHA1Managed();
            System.Security.Cryptography.SHA256Managed sha256managed = new System.Security.Cryptography.SHA256Managed();
            System.Security.Cryptography.SHA384Managed sha384managed = new System.Security.Cryptography.SHA384Managed();
            System.Security.Cryptography.SHA512Managed sha512managed = new System.Security.Cryptography.SHA512Managed();

            // ハッシュ/名前配列
            System.Security.Cryptography.HashAlgorithm[] hashes = { md5, sha1, sha1managed, sha256managed, sha384managed, sha512managed };
            string[] hashes_names = { "md5", "sha1", "sha1managed", "sha256managed", "sha384managed", "sha512managed" };

            // 処理時間計測用
            System.DateTime time_begin, time_end;

            Console.WriteLine(string.Format("Hash Algorithm calculation test, EACHSIZE={0}, {1} times calculation.",EACHSIZE,MAXTRY));

            // ハッシュ入力とするランダムデータ
            byte[][] source = new byte[MAXTRY][];

            Console.WriteLine("Creating random data...");
            for (int i = 0; i < MAXTRY; i++)
            {
                source[i] = new byte[EACHSIZE];
                rnd.NextBytes(source[i]);
            }
            Console.WriteLine("done.\n");


            // 各ハッシュ関数についてループ
            for (int i = 0; i < hashes.Length; i++)
            {
                System.Security.Cryptography.HashAlgorithm hasher = hashes[i];
                string hasher_name = hashes_names[i];
                byte[] computedhash = null;

                Console.WriteLine("Calculating " + hasher_name + " Hash for random data...");

                time_begin = DateTime.Now;

                for (int j = 0; j < MAXTRY; j++)
                {
                    byte[] input = source[j];
                    computedhash = hasher.ComputeHash(input);
                }

                time_end = DateTime.Now;
                TimeSpan ts = time_end - time_begin;
                Console.WriteLine(string.Format(" Calculate time: {0}ms, {1}ms per calculation", ts.TotalMilliseconds, ts.TotalMilliseconds/MAXTRY));

                Console.WriteLine("done.\n");

            }

            Console.WriteLine("test finished.");
            Console.ReadKey(false);
        }

    }
}