Skip to content

Latest commit

 

History

History
48 lines (33 loc) · 892 Bytes

06-parallelism.md

File metadata and controls

48 lines (33 loc) · 892 Bytes

06 - Parallelism

Goal

Use multiple CPU cores for CPU-bound work.

Task

Implement prime counting.

public interface IPrimeCounter
{
    int CountPrimes(int fromInclusive, int toInclusive);
}

Create:

  • SequentialPrimeCounter
  • ParallelForPrimeCounter
  • PlinqPrimeCounter

Step-by-step

  1. Implement a simple IsPrime method.
  2. Count primes sequentially.
  3. Use Parallel.For.
  4. Use PLINQ.
  5. Benchmark each approach.
  6. Try different degrees of parallelism.

Benchmark parameters

  • 1 to 10,000
  • 1 to 100,000
  • 1 to 1,000,000

Questions to answer

  • 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?

Key takeaway

Parallelism helps when the work is CPU-heavy enough to justify scheduling and coordination overhead.