Performance improvement to decrease startup time.

This commit is contained in:
mlemanczyk committed 2025-10-10 22:48:28 +02:00
1 parent 38120fba13
commit ee2df92a15
1 file changed
+26 -19
@@ -112,9 +112,12 @@ public static class MersenneNumberDivisorByDivisorTester
ulong maxPrime = 0UL; ulong maxPrime = 0UL;
int primeWriteIndex = 0; int primeWriteIndex = 0;
using IEnumerator<ulong> primeEnumerator = Prime.Numbers.GetEnumerator(); // This implementation is terribly slow, while this method expect prime p given as --filter-p input already.
bool hasPrime = primeEnumerator.MoveNext(); // We don't need to additionally check it.
ulong currentPrime = hasPrime ? primeEnumerator.Current : 0UL;
// using IEnumerator<ulong> primeEnumerator = Prime.Numbers.GetEnumerator();
// bool hasPrime = primeEnumerator.MoveNext();
// ulong currentPrime = hasPrime ? primeEnumerator.Current : 0UL;
for (int index = startIndex; index < candidateCount; index++) for (int index = startIndex; index < candidateCount; index++)
{ {
@@ -127,25 +130,29 @@ public static class MersenneNumberDivisorByDivisorTester
continue; continue;
} }
while (hasPrime && currentPrime < candidate) // This implementation is terribly slow, while this method expect prime p given as --filter-p input already.
{ // We don't need to additionally check it.
hasPrime = primeEnumerator.MoveNext();
if (!hasPrime) // while (hasPrime && currentPrime < candidate)
{ // {
break; // hasPrime = primeEnumerator.MoveNext();
} // if (!hasPrime)
// {
// break;
// }
currentPrime = primeEnumerator.Current; // currentPrime = primeEnumerator.Current;
} // }
if (!hasPrime || currentPrime != candidate) // if (!hasPrime || currentPrime != candidate)
{ // if (!Number.IsPrime(candidate))
markComposite(); // {
printResult(candidate, false, false, false); // markComposite();
continue; // printResult(candidate, false, false, false);
} // continue;
// }
primesSpan[primeWriteIndex++] = candidate; primesSpan[primeWriteIndex++] = candidate;
if (candidate > maxPrime) if (candidate > maxPrime)
{ {