この小さなコンソール アプリケーションは BigInteger をカウントし、ヒットした指数をフィードバックします。
今、私はいくつかの速度の改善に興味があります。私に何ができる?
あなたの提案をありがとう!
using System;
using System.Collections.Generic;
using System.Numerics;
namespace Counter
{
internal class Program
{
private static readonly Dictionary<BigInteger, int> Dic = new Dictionary<BigInteger, int>();
private static void Main(string[] args)
{
Console.WriteLine("Start with counting ... from 1 to 2^256.");
Console.WriteLine();
CreateDict();
var bigInteger = new BigInteger();
Console.WriteLine("D:HH:mm:ss,ms - fac. - Number");
Console.WriteLine("---------------------------------------------------");
var startTime = DateTime.UtcNow;
while (true)
{
bigInteger++;
if (Dic.ContainsKey(bigInteger))
{
Console.WriteLine("{0:G} - 2^{1,3} = {2:#,0}", (DateTime.UtcNow - startTime), Dic[bigInteger], bigInteger);
}
}
}
private static void CreateDict()
{
for (int i = 1; i <= 256; i++)
{
Dic.Add(BigInteger.Pow(2, i), i);
}
}
}
}
出力: http://pastebin.com/bMBntFsL
進捗
BigInteger での作業はあまり良くありませんでした。
BigInteger 2^26 = 5 秒
ダブル 2^26 = 1,3 秒
Dict から直接比較への切り替えは、
int i = 1;
double pow = Math.Pow(2, i);
while (true)
{
bigInteger++;
if (bigInteger == pow)
{
Console.WriteLine("{0:G} - 2^{1,3} = {2:#,0}", (DateTime.UtcNow - startTime), Dic[bigInteger], bigInteger);
i++;
pow = Math.Pow(2, i);
}
}
辞書 2^26 = 1,3 秒
"<" 2^26 = 0,5 秒