Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do coroutines improve performance

I've been seeing a lot of talks and articles on coroutines in python. They are considered to be "microthreads" and I've heard they improve performance.

How do coroutines improve performance? From what I've seen so far, they are single threaded and execute in sequence. I agree that from a design perspective they are quite elegant especially in producer/consumer type applications.

I guess I am missing the point. Can someone help explain?

like image 372
Raj N Avatar asked Jul 04 '11 17:07

Raj N


People also ask

Why are coroutines useful?

The advantages of coroutines over threads are that they may be used in a hard-realtime context (switching between coroutines need not involve any system calls or any blocking calls whatsoever), there is no need for synchronization primitives such as mutexes, semaphores, etc.

Why coroutines are faster?

Anyway, they're faster because they're just function calls with some overhead, whereas threads are constantly being switched between by the OS so they all get a share of CPU time.

Are coroutines faster?

When the number of Thread and Coroutine is very small, Thread is faster. However, when the number becomes bigger, Coroutine is much faster.

Are coroutines performant?

There's no difference in performance to when you're using coroutines in a PC or Mobile. Coroutines are basically a method that can be paused for the time of whatever yield instruction you pass to it. The only difference you may see is that the impact of a bad implatation is a lot more noticeble in low end devices.


2 Answers

Coroutines don't really improve performance except in a very limited sense: multi-threaded programs have a certain overhead and coroutines provide some of the benefit of threading without incurring those overheads. However, most multi-threaded applications (even in C-Python with its GIL) benefit from the overlap when one thread blocks on a system call and other threads can run: that doesn't generally happen with coroutines.

If you have a few threads then usually the overlapping wins out and coroutines give no performance benefit. If you need thousands of threads then the thread switch overheads will be much greater and in that situation coroutines might give a benefit but reducing the number of threads is likely to give much greater benefit.

The real gain of coroutines is that for producer/consumer applications they make the coding much simpler and therefore faster to code.

like image 132
Duncan Avatar answered Sep 28 '22 12:09

Duncan


This is a good question, it reminded me of David Beazley's A Curious Course on Coroutines and Concurrency. David does an excellent job of not only explaining how coroutines work in Python, but the use cases where they're really in the pocket.

His writing seems to indicate that the performance benefit comes from having less overhead to accomplish the same tasks for which you'd typically use a handler class (see slide 51 of his presentation).

So like @Duncan's answer suggests, situations where overhead matters (like having many many threads), coroutines are a performance win, but coroutines are about a lot more than just performance.

like image 23
aaronstacy Avatar answered Sep 28 '22 13:09

aaronstacy