IEnumerable
nie będzie działać. Optymalizacja niektórych przykładach w tym temacie:
Pierwszy z nich (najszybsza - 2,35 sekundy dla 10M tras, zakres 1..10M):
static uint[] MulDeBruijnBitPos = new uint[32]
{
0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
};
static uint[] GetExponents(uint value)
{
uint[] data = new uint[32];
int enabledBitCounter = 0;
while (value != 0)
{
uint m = (value & (0 - value));
value ^= m;
data[enabledBitCounter++] = MulDeBruijnBitPos[(m * (uint)0x077CB531U) >> 27];
}
Array.Resize<uint>(ref data, enabledBitCounter);
return data;
}
innej wersji (drugi najszybszy - 3 sekund 10M tras, zakres 1..10M):
static uint[] GetExponents(uint value)
{
uint[] data = new uint[32];
int enabledBitCounter = 0;
for (uint i = 0; value > 0; ++i)
{
if ((value & 1) == 1)
data[enabledBitCounter++] = i;
value >>= 1;
}
Array.Resize<uint>(ref data, enabledBitCounter);
return data;
}
z własnego doświadczenia Math.pow jest bardzo powolny. Znacznie wolniej niż np. Math.Cos lub Math.Sqrt. Nie ma szans na lepsze wyniki niż kiedykolwiek. –