Do you know why sync fibonacci method is faster than async/await and that is faster than async task?
I used async on every project method, so it\'s main this is so b
a good rule of thumb: only use async calls for functions that use an external resource: file system, database, http calls, ...
when you do in memory stuff, like calculating fibonacci, do it sync, the overhead of creating a seperate thread/context for in memory calculation is too much
unless you have a ui thread that is waiting for the calculation to be finished, you can implement it async, but be aware you will indeed have some performance loss ...
Do you know why sync fibonacci method is faster than async/await and that is faster than async task?
Asynchrony is not about improving raw speed. As you've discovered, it takes longer overall. If you use it poorly, as you have done, it makes things much, much slower for zero benefit.
The fundamental problem here is that you don't understand what asynchrony is for. Asynchrony is for managing latency. Once you internalize that fact, you'll start using it correctly.
Latency is the gap in time between when you request a computation or side effect, and when the computation or side effect is complete.
For example, suppose you are computing something that is extremely computationally expensive. You're computing a really complicated graphic, for example, and it is going to take more than 30 milliseconds to compute even if you dedicate an entire core to it. You do not want your user interface to stall while you're doing the computation, so you can put the computation onto another thread, dedicate a CPU to that thread, and await the result. An await means "go find more work to do while I am waiting for a high latency operation to complete".
For example, suppose you are doing something that is not computationally expensive but requires waiting on an external resource. You're calling into a database, for example, and it is going to take more than 30 ms to get the result back. In this case you do not want to spin up a thread. That thread is just going to sleep waiting for the result! Instead you want to use the async version of the database access API, and await the high latency result.
In your example you don't have a high-latency operation to begin with, so it makes no sense to await it. All you're doing there is creating a lot of work for the runtime to create and manage the work queues, and that is work you are paying for but getting no benefit from. Your work takes nanoseconds; only use asynchrony for jobs that take milliseconds or longer.
Only use async when you have a high-latency operation. Async improves performance because it frees up a thread to keep on doing work while it is waiting for a high-latency result to appear. If there is no high-latency result then async will just slow everything down.