gpt4 book ai didi

asynchronous - 如何测量 Julia 中函数的@async 运行时间?

转载 作者:行者123 更新时间:2023-12-04 01:12:19 25 4
gpt4 key购买 nike

我的代码如下,

function getImages()
@async for i in 1:30
load("path/to/image$i.jpg");
end
end

@time @sync getImages()

这是在 Julia 中测量异步运行时的正确方法吗?我不想相信它,因为在同步运行中它需要 0.1 秒才能完成,而使用 @async 代码它只显示 0.000017 秒。

最佳答案

你需要稍微重新组织一下你的代码

julia> function getImages()
@sync for i in 1:30
@async begin
sleep(rand())
println("Image ", i)
end
end
end
getImages (generic function with 1 method)

julia> @time getImages()
Image 2
Image 5
Image 15
Image 30
Image 4
Image 7
Image 20
Image 3
Image 22
Image 18
Image 11
Image 29
Image 9
Image 24
Image 16
Image 6
Image 8
Image 14
Image 19
Image 21
Image 13
Image 1
Image 17
Image 10
Image 27
Image 25
Image 23
Image 28
Image 26
Image 12
0.894566 seconds (650 allocations: 37.406 KiB)

关于asynchronous - 如何测量 Julia 中函数的@async 运行时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64454237/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com