linux-sysprog · beginner · ~15 min
Estimate perfect-scaling runtime.
Under perfect scaling, splitting serial work across N threads cuts the runtime to roughly serial/N (rounded up). Estimate that ideal time.
Implement long ideal_runtime(long serial_ms, int threads) that returns the perfectly-scaled runtime.
serial_ms: the single-threaded runtime in milliseconds.threads: number of threads.ceil(serial_ms / threads), or serial_ms unchanged if threads <= 1.
ideal_runtime(1000, 4) -> 250
ideal_runtime(1000, 3) -> 334 (ceiling)
ideal_runtime(500, 1) -> 500
threads <= 1: return serial_ms unchanged.serial_ms: single-threaded runtime; threads: thread count.
Ceiling of serial_ms / threads, or serial_ms if threads <= 1.
threads <= 1 returns serial_ms unchanged.
long ideal_runtime(long serial_ms, int threads) {
/* TODO */
return serial_ms;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.