CPU Bound vs I/O Bound
Bharath Muppa
Posted on September 27, 2021
Before Understanding what is CPU Bound and IO Bound, I would like to touch the basic understanding of how CPU and IO work, then slowly dwell into the above concepts.
There is a simple analogy to understand in a better way.
Let's assume you want to start a Huge Restaurant(computer).
There are primarily 2 Requirements
- you need a chef(s) π¨βπ³
- Warehouse for food Storageπ
1. Hire a Chef (Buy CPU)
The chef is the one who is responsible for all your recipes.
- Depending on your requirements, you have to hire a chef(CPU). If you spend more π€ you will get a master chef and he can add more recipes to the menu, cooks fast, reduce cost with his experience.
- Let's say your city is famous for Shrimps, So you have to hire a chef who is specialized in making different shrimps (GPU).
Tasks of CPU
1. Fetch instructions from memory
2. Decode into binary instructions
3. Execute action and move to next step
4. Write output to memory
2. Establish a Delivery system.
- π½ As we need to store all the groceries and food items required for recipes beforehand, we need warehouses (can be RAM, HardDisk, USB, Flash Drive)
- π We also need a transport system to transport data (PCIe express, SATA, Data Bus).
- you also need to establish a connection with dealers at the market to get fresh food/shrimps at a low price. (Network calls, Mounted Drives)
Ok, you are good to start a Restaurant now.
Let's think of 2 scenarios.
Your Restaurant becomes famous and getting more orders, so you need a chef to chop some millions of vegetables for a thousand orders. If your chef is slow you can't deliver what customers want on time, so you need a fast chef to chop all the vegetables required and prepare the recipe.
Your chef needs a thousand food items and millions of groceries to prepare food. This is not the concern of the chef, this is the responsibility of all others it might be warehouse storage, market guys and transport persons.
I think it is quite a simple scenario. If yes you already understand what is CPU bound and IO Bound.
If you didn't really understand the anaology..no need to worry, we can simplify it by Demystifying the analogy
Analogy | Reality |
---|---|
Restaurant | Whole Computer |
Chef | Processor |
4 handed Chef | Quad-core Processor |
Recipes | Threads |
Deliver system | All other than processor |
Special Shrimp Chef | GPU or FPGA or TPU |
Without Analogy, in simple terms.
CPU Bound
We can say a program/language is CPU Bound if it has to
Example application: Photo Editors, Gaming, Video Editors
IO Bound
We can say a program/language is IO Bound if it has to do
- file reading/writing
- Do a Network call or responds to more network calls
Example application: Chat applications, Feeds, Bank applications
NOTE
It doesn't mean if an application is CPU bound, then it should not do any IO Operations and viceversa
Now everyone is on the same page and hopefully understood core concepts.
Now entering into an Opinionated Zoneβ‘
Lets put our knowledge into a matrix while choosing a language if you are asked to build an application.
Note
- I am considering out of the box architecture of corresponding languages. As every language has a possibility to use thread pools, multi processes, multi-threads.
- I want to exclude c and c++ because of their power and also theirs difficulty in creating the application for beginners.
Application | Language |
---|---|
Chat app | Node |
Data-Intensive | Java, c# |
Photoshop | Java, c# |
Web Scrapping | Python, Node |
Reference
- Credits for this analogy should be given to (David Xiang)[https://twitter.com/davex_tech] and his book Software Developer Life: Career, Learning, Coding, Daily Life, Stories
- Introduction to threading
Posted on September 27, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.