I am running into an issue where allowing multi-threading leads to slower execution time. Following is the simple code where I am testing it:
function sqr(x) return x*x end y = zeros(10) for i=1:8 y[i] = sqr(i) end
0.008442 seconds (18.07 k allocations: 1.085 MiB)
Now, allowing multi-threading (8 threads) using
function sqr(x) return x*x end y = zeros(10) Threads.@threads for i=1:8 y[i] = sqr(i) end
0.038529 seconds (20.16 k allocations: 1.068 MiB)
The code utilizing 8 threads is considerably slower than the one using just 1 thread. This seems to be counter intuitive.
Any suggestions/reasoning on why this could be happening and how to correct it? I am using Julia 1.5.0.