Single-threaded Speed-up

The Julia scheduler is running on thread 1. If your application uses tasks or channels on thread 1 (e.g. with process!), it has to compete against other background tasks. You can remove this competition and speed up your application significantly (often by $\times10$ or more) by moving it to a thread other than 1.

onthread(f::F, tid::Int; wait::Bool=true) where {F<:Function}

Execute a function f on thread tid.

To execute f on a thread other than 1 can speed it up significantly if it depends on asynchronous tasks.


  • f::Function: function to execute
  • tid::Int: thread id
  • wait::Bool=true: if true, it waits for function to finish


julia> using DiscreteEvents, .Threads

julia> onthread(2) do; threadid(); end

Look at the M/M/c benchmarks and to this example on DiscreteEventsCompanion for an illustration. You cannot speedup applications not using tasks or channels with this technique.

Parallel RNG Seed


Seed each of the thread local RNGs with s*threadid() to get reproducible, but different random number sequences on each thread.