this small console application count a BigInteger and give me a feedback which exponent it hits.
Now I'm curious for some speed improvments. What can I do?
Thx for your suggestions!
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);
}
}
}
}
output: http://pastebin.com/bMBntFsL
Progress
Working with BigInteger was not so good.
BigInteger 2^26 = 5s
Double 2^26 = 1,3s
Switching from Dict to direct compare was much faster to
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);
}
}
Dict 2^26 = 1,3s
"<" 2^26 = 0,5s