CPU Bound vs I/O Bound
Tags: computerscience, operatingsystem, codenewbie, beginners
Published: 2021-09-27 17:00:15

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__
1.  __you need a chef(s)__ 👨‍🍳
2. __Warehouse for food Storage__🏭    

## __1. Hire a Chef (Buy CPU)__

The chef is the one who is responsible for all your recipes.    

![Giffy](https://dev-to-uploads.s3.amazonaws.com/i/5khlxlearu00n6fg8isp.gif)

* 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).

![Big Bang Theory](https://dev-to-uploads.s3.amazonaws.com/i/32eykw241t6056sf276g.gif)

> __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.__ 

1. 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. 

2. 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      |

![Overview of processing units](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ev1f4g1o6hn0pu53swa.jpg)

Without Analogy, in simple terms.
### __CPU Bound__  
We can say a program/language is __CPU Bound__ if it has to
1. process more Data [CPU](https://computer.howstuffworks.com/microprocessor.htm)
2. processing audio or video [GPU](https://computer.howstuffworks.com/graphics-card1.htm)
3. Processing vector instructions ([VPU](https://www.geekboots.com/story/what-is-vpu)) 

__Example application__: _Photo Editors, Gaming, Video Editors_

### __IO Bound__ 
We can say a program/language is IO Bound if it has to do
1. file reading/writing
2. 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⚡__
![Giphy](https://dev-to-uploads.s3.amazonaws.com/i/3ded72wplnmf4npu76e9.gif)
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](https://www.amazon.com/dp/B07D5QNGVZ/r)
* [Introduction to threading](https://www.youtube.com/watch?v=zphcsoSJMvM)