Debugging NetLogo:使用局部变量“节省或增加计算时间”;“让我来吧?”;?
我想节省海龟移动的计算时间(问题贴在这里:)。在最初的移动海龟程序中,作者使用了许多“let”局部变量。我想我可以很容易地用内置的NetLogo原语p.ex替换这些“let”变量。在这里:Debugging NetLogo:使用局部变量“节省或增加计算时间”;“让我来吧?”;?,debugging,time,netlogo,Debugging,Time,Netlogo,我想节省海龟移动的计算时间(问题贴在这里:)。在最初的移动海龟程序中,作者使用了许多“let”局部变量。我想我可以很容易地用内置的NetLogo原语p.ex替换这些“let”变量。在这里: ; original code with "let" local variables let np patches in-radius 15 ; define your perceptual range
; 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”局部变量会节省计算时间还是会更耗时?我怎样才能轻松地验证它?谢谢你抽出时间
(注:在对我前面的问题进行评论之后,我想原语变量会更有效,我只是更愿意确定)显然,[]中的局部变量比原语变量工作得更快 仅比较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
; )
vs2)使用局部变量,然后计算海龟移动
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 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
; )
所以我想在耗时的计算中使用局部变量
如果我错了,请你纠正我的结论,我将不胜感激。谢谢 差异在于每个报告者的计算次数。如果你说
让np面片在半径15
内,那么这实际上计算了15个距离内的面片数,并将该值赋给名为np
的变量。在计算中使用np
直接替换保存的值。如果您必须在代码中使用它10次,那么使用let
意味着它只需计算一次并读取10次。或者,如果不将其存储在变量中,则需要在radius 15中的10个不同位置使用补丁,每次NetLogo都需要计算该值。请参见Jen的答案。如果使用let
意味着避免重复相同的计算,则可以节省时间。否则,你不会。