Skip to content

Instantly share code, notes, and snippets.

@Arkoniak
Created February 21, 2020 08:58
Show Gist options
  • Save Arkoniak/6a2d50739be13fc35910d6fa8c4ab332 to your computer and use it in GitHub Desktop.
Save Arkoniak/6a2d50739be13fc35910d6fa8c4ab332 to your computer and use it in GitHub Desktop.
using BenchmarkTools
using Distributed
using Base.Threads
addprocs(4)
nworkers() # should be 4
function f1()
nheads = @distributed (+) for i = 1:200000000
Int(rand(Bool))
end
nheads
end
function f2()
nheads = 0
for i = 1:200000000
nheads += Int(rand(Bool))
end
nheads
end
function f3()
wait_list = Task[]
for i in 1:3
push!(wait_list, Threads.@spawn f4())
end
nheads = f4()
for i in 1:3
nheads += Threads.fetch(wait_list[i])
end
nheads
end
function f4()
nheads = 0
for i in 1:50000000
nheads += Int(rand(Bool))
end
nheads
end
@btime f1()
@btime f2()
@btime f3()
@Arkoniak
Copy link
Author

In my setup (4 nworkers, 4 nthreads)
f1: 370.550 ms (824 allocations: 33.41 KiB)
f2: 1.501 s (0 allocations: 0 bytes)
f3: 388.202 ms (30 allocations: 2.44 KiB)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment