netlogo - Efficiency clarification: Asking subsets of a general turtle type or asking breeds? -
i'm working large model many agents executing many procedures each day.
is better such:
ask turtles [ if type = "a" [do-a-thing] if type = "b" [do-b-thing] ]
or alternatively
ask a-turtles [do-a-thing] ask b-turtles [do-b-thing]
i'm looking lower computational load.
here's example tries both of approaches. creates 100,000 turtles , executes test 1,000 times each case. approach 2 seems faster. on machine, approach 1 took 46.3 seconds, , approach 2 took 30.6 seconds. doesn't use ifelse in approach 1. seems logical approach 2 faster, since avoid comparisons.
breed[a-turtles a-turtle] breed[b-turtles b-turtle] turtles-own [ typ ] ; setup button callback function setup clear-all ; create turtles approach 1 create-turtles 100000 ask turtles [ ifelse (who < 50000) [ set typ "a" ] [ set typ "b" ] ] ; time approach 1 let 0 let iter 1000 reset-timer while [ < iter ] [ ; approach 1 ask turtles [ if typ = "a" [ dosomething ] if typ = "b" [ dosomething ] ] set i + 1 ] print (word "approach 1 time: " timer " secs") ; delete turtles, create turtles approach 2 clear-all clear-turtles create-a-turtles 50000 create-b-turtles 50000 ; time approach 2 set 0 reset-timer while [ < iter ] [ ; approach 2 ask a-turtles [ dosomething ] ask b-turtles [ dosomething ] set i + 1 ] print (word "approach 2 time: " timer " secs") end dosomething let 0 set i + 1 set i - 1 end
Comments
Post a Comment