朱莉娅的全局变量使代码变慢较慢
我有以下在大约8秒内运行的代码:
using Random
DEBUG = false
PRISONERS = 100
ATTEMPTS = 50
function tryit()
a = [1:1:PRISONERS;]
a = shuffle(a)
for i in 1:PRISONERS
if DEBUG
println("prisoner $i")
end
count = 0
check = i
while count <= ATTEMPTS
count += 1
if a[check] == i
if DEBUG
println("Attempt $count: Checking box $check and found my number")
end
break
else
if DEBUG
println("Attempt $count: Checking box $check and found $(a[check])")
end
check = a[check]
end
end
if count > ATTEMPTS
if DEBUG
println("Prisoner $i failed to find his number in 50 attempts")
end
return false
end
end
return true
end
function main()
tries = 100000
success = 0.0
for i in 1:tries
if tryit()
success += 1
end
end
println("Ratio of success = $(success / tries)")
end
main()
将函数内部的全局变量移动将运行时间降低到第十:
using Random
function tryit()
DEBUG = false
PRISONERS = 100
ATTEMPTS = 50
a = [1:1:PRISONERS;]
a = shuffle(a)
for i in 1:PRISONERS
if DEBUG
println("prisoner $i")
end
count = 0
check = i
while count <= ATTEMPTS
count += 1
if a[check] == i
if DEBUG
println("Attempt $count: Checking box $check and found my number")
end
break
else
if DEBUG
println("Attempt $count: Checking box $check and found $(a[check])")
end
check = a[check]
end
end
if count > ATTEMPTS
if DEBUG
println("Prisoner $i failed to find his number in 50 attempts")
end
return false
end
end
return true
end
function main()
tries = 100000
success = 0.0
for i in 1:tries
if tryit()
success += 1
end
end
println("Ratio of success = $(success / tries)")
end
main()
我无法用较小的代码示例来重现问题。我在Linux Mint上使用Julia 1.7.2版。
I have the following code that runs in about 8 seconds:
using Random
DEBUG = false
PRISONERS = 100
ATTEMPTS = 50
function tryit()
a = [1:1:PRISONERS;]
a = shuffle(a)
for i in 1:PRISONERS
if DEBUG
println("prisoner $i")
end
count = 0
check = i
while count <= ATTEMPTS
count += 1
if a[check] == i
if DEBUG
println("Attempt $count: Checking box $check and found my number")
end
break
else
if DEBUG
println("Attempt $count: Checking box $check and found $(a[check])")
end
check = a[check]
end
end
if count > ATTEMPTS
if DEBUG
println("Prisoner $i failed to find his number in 50 attempts")
end
return false
end
end
return true
end
function main()
tries = 100000
success = 0.0
for i in 1:tries
if tryit()
success += 1
end
end
println("Ratio of success = $(success / tries)")
end
main()
Moving the global variables inside the function cuts the runtime to a tenth:
using Random
function tryit()
DEBUG = false
PRISONERS = 100
ATTEMPTS = 50
a = [1:1:PRISONERS;]
a = shuffle(a)
for i in 1:PRISONERS
if DEBUG
println("prisoner $i")
end
count = 0
check = i
while count <= ATTEMPTS
count += 1
if a[check] == i
if DEBUG
println("Attempt $count: Checking box $check and found my number")
end
break
else
if DEBUG
println("Attempt $count: Checking box $check and found $(a[check])")
end
check = a[check]
end
end
if count > ATTEMPTS
if DEBUG
println("Prisoner $i failed to find his number in 50 attempts")
end
return false
end
end
return true
end
function main()
tries = 100000
success = 0.0
for i in 1:tries
if tryit()
success += 1
end
end
println("Ratio of success = $(success / tries)")
end
main()
I could not reproduce the problem with a smaller code example. I am using Julia version 1.7.2 on Linux Mint.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么要全球?如果您担心,因为您想在某些原因下将变量的值保持在函数呼叫时的全局值,则将它们在函数签名中的混音可以解决速度降低问题:
Why globals? If you are concerned because you want to keep the variables' values global at function call for some reason, aliasing them in your function signature can fix the slowdown problem:
将全局变量声明为常数解决了问题。
参见为什么在非相互作用的朱莉娅中,全球变量和局部变量之间存在性能差异程序?用于技术解释。
Declaring the global variables as constants solved the problem.
See Why is there a performance difference between global and local variables in non-interactive Julia programs? for technical explanations.