Skip to content

Instantly share code, notes, and snippets.

@jpsamaroo
Created March 21, 2023 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpsamaroo/7da2124ce306ad6ae3e2588368bbdcda to your computer and use it in GitHub Desktop.
Save jpsamaroo/7da2124ce306ad6ae3e2588368bbdcda to your computer and use it in GitHub Desktop.
using BenchmarkTools
function bench_async_yield(N, T)
ts = [@async begin
for i in 1:N
yield()
end
end for _ in 1:T]
wait.(ts)
end
function bench_spawn_yield(N, T)
ts = [Threads.@spawn begin
for i in 1:N
yield()
end
end for _ in 1:T]
wait.(ts)
end
function bench_async_create_finish(N)
ts = [@async begin
end for _ in 1:N]
wait.(ts)
end
function bench_spawn_create_finish(N)
ts = [Threads.@spawn begin
end for _ in 1:N]
wait.(ts)
end
# 1 thread
# Task hooks:
# 36.6 ms
# Master:
# 35.5 ms
display(@benchmark bench_async_yield(10_000, 8))
# 3 threads
# Task hooks:
# 30.33 ms
# Master:
# 32.71 ms
display(@benchmark bench_spawn_yield(10_000, 8))
# 1 thread
# Task hooks:
# 3.47 ms
# Master:
# 3.71 ms
display(@benchmark bench_async_create_finish(10_000))
# 3 threads
# Task hooks:
# 4.05 ms
# Master:
# 3.85 ms
display(@benchmark bench_spawn_create_finish(10_000))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment