1
$\begingroup$

I am trying to understand the difference between CPU Bound vs IO Bound process. ChatGPT suggested that multi-threading/parallel processing can help CPU bound process; However, I think that having mulithreading can help with IO Bound process.

Let's say the application is talking with some API, and has to make various API calls. If we have multi-threading, we can make those API calls in parallel, and boost the perforamnce.

Is thre anything wrong with my understanding?

$\endgroup$

2 Answers 2

1
$\begingroup$

I've used multithreading for speeding up many tasks, but never for API calls. So I went ahead and wanted to make a python code to try to visualize multithreading in API.

Here is the src: pastebin link here

I made it request to my github's page via the githhub api and request 1,5,10,20, and 30 times so I can get more data. I made it run for 1 through 5 threads to see if the number of threads impacted performance.

The result is expected as when running I/O tasks, you don't have to waste time waiting for every single request to complete.

Image

For CPU tasks when you are trying to multithread what it ultimately comes down to is how well you can parallelized the tasks to get split. A scenario if you need practice to see this in action is initialize an array and perform a computation on each element in the array (this can be easily parallelized)

An example where you can't easily use multithreading is calculating the Fibonacci sequence using the recursive bad algorithm. (If you memoize the Fibonacci sequence when you produce it you can easily parallelize it)

$\endgroup$
4
  • $\begingroup$ A pity Async with 1 Thread isn't included. $\endgroup$ Commented Aug 2, 2024 at 16:06
  • $\begingroup$ (If you memoize the Fibonacci sequence when you produce it you can easily parallelize it care to show how to do so to any significant advantage?) $\endgroup$ Commented Aug 2, 2024 at 16:08
  • $\begingroup$ pastebin.com./8tA190Yx Sure, here is a matrix based fibonacci where the naive takes 7.5 seconds to generate the 30th element. The 50th element while the other method gets the 100th element in 0.00097 seconds. Code is attached above. I kind of cheated because I used different methods to generate but whatever. :// $\endgroup$ Commented Aug 3, 2024 at 20:11
  • $\begingroup$ No whatever: compare to FIB_MATRIX(N) for apples to apples. (I recommend to follow the Style Guide for Python Code more closely.) $\endgroup$ Commented Aug 3, 2024 at 20:30
1
$\begingroup$

It depends. If there is a single resource you are waiting on, and you cannot send multiple requests at a time, then multiple threads don't help. If you can send multiple requests, and they don't need to wait for each other, and the resource can handle more requests than you can send, then multiple threads can help.

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.