Threads.@threads ~44 times slower than single threaded code

using .Threads

function runthis()
    N=1000
    M=500000
    
    dt=0.01
   
    a=1
    
    
    data = zeros(N, nthreads())
    
    
    @threads for i=1:M
        x=0.0
        v=0.0
    
        for j=1:N
            noise=randn()/sqrt(dt)
          
            xnew=x + dt*(v)
            vnew=v + dt*(-a*x + noise)
       
            data[j, threadid()]  += (x*x)/M #to find <x(t)^2>
            x=xnew
            v=vnew
        end
    end  
   
    data_sum = sum(data, dims=2)

    f = open("output.txt", "w")
    for i = 1:N
        println(f, round(i*dt,digits=2)," ",data_sum[i])
    end
    close(f)

end

@time runthis()
@time runthis()

The above code is an attempt to remove the race condition. It seems to work well and tests slightly faster for me.

1 Like