Use multiple CPU cores for CPU-bound work.
Implement prime counting.
public interface IPrimeCounter
{
int CountPrimes(int fromInclusive, int toInclusive);
}Create:
SequentialPrimeCounterParallelForPrimeCounterPlinqPrimeCounter
- Implement a simple
IsPrimemethod. - Count primes sequentially.
- Use
Parallel.For. - Use PLINQ.
- Benchmark each approach.
- Try different degrees of parallelism.
- 1 to 10,000
- 1 to 100,000
- 1 to 1,000,000
- Is parallel always faster?
- What happens for small inputs?
- What happens when degree of parallelism is too high?
- What is the difference between CPU-bound and I/O-bound work?
Parallelism helps when the work is CPU-heavy enough to justify scheduling and coordination overhead.