Does @asyc or Threads.@spawn affect performance

To my understanding @async creates a closure which may box a variable. And the docs say boxing affects performance. Or is there something am getting wrong. ? (Am aware of the interpolation operator in julia >= 1.4).

Yes @async or @spawn has overhead. However if you have multiple tasks that can be done in parallel rather than serially, then that overhead is worth it.

If you are doing:

Threads.@spawn a = b  + c
Threads.@spawn d = e + f
Threads.@spawn g = h + i

You are not going to see any performance improvement since the work required to run those in parallel will outweigh the time needed to perform the addition. You probably don’t want to try to run tasks that will finish in less than a second in parallel.

The only way to truly know if you will get a performance improvement is to first write it in serial, debug it, benchmark it, then parallelize it, make sure you get the same result, and benchmark it.

2 Likes

Thanks