CPU or I/O bound?

Whether you use Kotlin Coroutines or RxJava, you are used to choose CPU or I/O bound schedulers. Do you know why? Let’s briefly sum up this topic.

CPU bound

CPU bound operation depends on CPU only. It is not waiting for any underlying hardware. Thus it’s incredibly fast and used for computation (e.g. image blurring, …)

  • RxJava: Schedulers.computation()
  • Coroutines: Dispatchers.Default

CPU bound scheduler can have as many threads as there is number of CPUs available. And they are used heavily for computation.

CPU bound algorithms benefits from parallelism, but not from single-core concurrency.

I/O bound

I/O bound operation usually takes longer since it’s waiting (IOWait) for underlying hardware (e.g. handling local file / database / network).

  • RxJava: io Scheduler
  • Coroutines: Dispatchers.IO

I/O bound schedulers can have as many threads as available resources (memory). I/O scheduler means a lot of threads. Majority of those threads are waiting for resources.

I/O bound will always perform better in concurrent implementation than if they are sequential.

Interchange them?

What happen if you use one instead of the other?

Using I/O operations on CPU scheduler? It will block limited thread pool and I/O operations will be slower.

Using computation on I/O scheduler? It will overloads CPU cores and thus slowdown computation.

Create a website or blog at WordPress.com

Up ↑