NetLogo:使用局部变量 "let" 来节省或增加计算时间?
NetLogo: save or increase calculation time with using local variable "let"?
我想节省乌龟移动的计算时间(此处发布的问题:NetLogo: how to make the calculation of turtle movement easier?)。在最初的 move-turtles 过程中,作者使用了许多 "let" - 局部变量。我想我可以很容易地用内置的 NetLogo 基元 p.ex 替换这些 "let" 变量。这里:
; original code with "let" local variables
let np patches in-radius 15 ; define your perceptual range
let bnp max-one-of np [totalattract] ; max of [totalattract] of patches in your neighborhood
let ah [totalattract] of patch-here ; [totalattract] of my patch
let xcorhere [pxcor] of patch-here
let ycorhere [pycor] of patch-here
let abnp [totalattract] of bnp
ifelse abnp - ah > 2 [ ...
这个条件可以代替吗?
; make the same condition with NetLogo primitives
ifelse ([totalattract] of max-one-of patches in-radius 15 [totalattract] - [totalattract] of patch-here > 2 [ ...
请问,使用 "let" 局部变量会节省计算时间还是会更耗时?我怎样才能轻松验证它?感谢您的时间 !
(PS:根据对我之前问题的评论,我认为基元变量会更有效率,我只是更愿意确定)
显然 [] 中的局部变量比原语 NetLogo 变量工作得更快。
比较1) 仅 NL 原语
let flightdistnow sqrt (
; (([pxcor] of max-one-of patches in-radius 15 [totalattract] - [pxcor] of patch-here ) ^ 2) +
; ([pycor] of max-one-of patches in-radius 15 [totalattract] - [pycor] of patch-here ) ^ 2
; )
vs 2) 使用局部变量然后计算海龟移动
to move-turtles
let np patches in-radius 15 ; define your perceptual range
let bnp max-one-of np [totalattract] ; max of [totalattract] of patches in your neighborhood
let ah [totalattract] of patch-here ; [totalattract] of my patch
let xcorhere [pxcor] of patch-here
let ycorhere [pycor] of patch-here
let abnp [totalattract] of bnp
ifelse abnp - ah > 2 [
move-to bnp ; move if attractiveness of patches-here is lower then patches in-radius
let xbnp [pxcor] of bnp
let ybnp [pycor] of bnp
let flightdistnow sqrt ((xbnp - xcorhere) * (xbnp - xcorhere) + (ybnp - ycorhere) * (ybnp - ycorhere))
set t_dispers (t_dispers + flightdistnow)
set energy (energy - (flightdistnow / efficiency))
set flightdist (flightdist + flightdistnow)
; if ([pxcor] of patch-here = max-pxcor) or ([pycor] of patch-here = max-pycor) or ([pxcor] of patch-here = min-pxcor) or ([pycor] of patch-here = min-pycor)
; [set status "lost"
; set beetle_lost (beetle_lost + 1)]
] ; if attractivity of [totalattract] is higher the the one of my patch
并使用秒表观察 5000 只海龟的移动我的结果是:
- 1) 10 秒
- 2) 5 秒
所以我想在耗时的计算中使用局部变量。
如果我的结论有误,请您更正我的结论,我将不胜感激。谢谢!!
区别在于每个报告者被计算的次数。如果你说 let np patches in-radius 15
那么实际上会计算 15 个距离内的补丁数并将该值赋给名为 np
的变量。在计算中使用 np
直接替换保存的值。如果你必须在你的代码中使用它 10 次,那么使用 let
意味着它被计算一次并简单地读取 10 次。或者,如果您不将它存储在变量中,那么您将需要在代码中的 10 个不同位置 patches in-radius 15
,NetLogo 每次都需要计算该值。
我想节省乌龟移动的计算时间(此处发布的问题:NetLogo: how to make the calculation of turtle movement easier?)。在最初的 move-turtles 过程中,作者使用了许多 "let" - 局部变量。我想我可以很容易地用内置的 NetLogo 基元 p.ex 替换这些 "let" 变量。这里:
; original code with "let" local variables
let np patches in-radius 15 ; define your perceptual range
let bnp max-one-of np [totalattract] ; max of [totalattract] of patches in your neighborhood
let ah [totalattract] of patch-here ; [totalattract] of my patch
let xcorhere [pxcor] of patch-here
let ycorhere [pycor] of patch-here
let abnp [totalattract] of bnp
ifelse abnp - ah > 2 [ ...
这个条件可以代替吗?
; make the same condition with NetLogo primitives
ifelse ([totalattract] of max-one-of patches in-radius 15 [totalattract] - [totalattract] of patch-here > 2 [ ...
请问,使用 "let" 局部变量会节省计算时间还是会更耗时?我怎样才能轻松验证它?感谢您的时间 !
(PS:根据对我之前问题的评论,我认为基元变量会更有效率,我只是更愿意确定)
显然 [] 中的局部变量比原语 NetLogo 变量工作得更快。
比较1) 仅 NL 原语
let flightdistnow sqrt (
; (([pxcor] of max-one-of patches in-radius 15 [totalattract] - [pxcor] of patch-here ) ^ 2) +
; ([pycor] of max-one-of patches in-radius 15 [totalattract] - [pycor] of patch-here ) ^ 2
; )
vs 2) 使用局部变量然后计算海龟移动
to move-turtles
let np patches in-radius 15 ; define your perceptual range
let bnp max-one-of np [totalattract] ; max of [totalattract] of patches in your neighborhood
let ah [totalattract] of patch-here ; [totalattract] of my patch
let xcorhere [pxcor] of patch-here
let ycorhere [pycor] of patch-here
let abnp [totalattract] of bnp
ifelse abnp - ah > 2 [
move-to bnp ; move if attractiveness of patches-here is lower then patches in-radius
let xbnp [pxcor] of bnp
let ybnp [pycor] of bnp
let flightdistnow sqrt ((xbnp - xcorhere) * (xbnp - xcorhere) + (ybnp - ycorhere) * (ybnp - ycorhere))
set t_dispers (t_dispers + flightdistnow)
set energy (energy - (flightdistnow / efficiency))
set flightdist (flightdist + flightdistnow)
; if ([pxcor] of patch-here = max-pxcor) or ([pycor] of patch-here = max-pycor) or ([pxcor] of patch-here = min-pxcor) or ([pycor] of patch-here = min-pycor)
; [set status "lost"
; set beetle_lost (beetle_lost + 1)]
] ; if attractivity of [totalattract] is higher the the one of my patch
并使用秒表观察 5000 只海龟的移动我的结果是:
- 1) 10 秒 - 2) 5 秒
所以我想在耗时的计算中使用局部变量。
如果我的结论有误,请您更正我的结论,我将不胜感激。谢谢!!
区别在于每个报告者被计算的次数。如果你说 let np patches in-radius 15
那么实际上会计算 15 个距离内的补丁数并将该值赋给名为 np
的变量。在计算中使用 np
直接替换保存的值。如果你必须在你的代码中使用它 10 次,那么使用 let
意味着它被计算一次并简单地读取 10 次。或者,如果您不将它存储在变量中,那么您将需要在代码中的 10 个不同位置 patches in-radius 15
,NetLogo 每次都需要计算该值。