Optimization List - NetLogo/NetLogo GitHub Wiki
This page lists all optimized primitives in NetLogo and includes a summary of which constructs are captured, how they are transformed, and the performance benefits of these transformations.
Forward 1
- Sample NetLogo Code:
forward 1
,fd 1
- Transformation: Changes to an optimized
_fd1
primitive which has 1 hardcoded - Performance changes: Avoids setting a temporary let variable, skips evaluation of argument
Sprout with no command block
- Sample NetLogo Code:
sprout 1
,sprout-wolves 10
- Transformation: Changes to an optimized
_sproutfast
which doesn't use a command block. - Performance changes: Avoids having to run separate jobs for each created agent.
Forward less than 1
- Sample NetLogo Code: forward 0
- Transformation: Changes to an optimized
_jump
which causes turtle to not move at all - Performance changes: Avoids doing computations necessary for a turtle to move.
HatchFast
- Sample Netlogo Code:
hatch 1
,hatch-sheep 1
- Transformation: Changes to an optimized
_hatchfast
without a command block - Performance changes: Avoids having to run separate jobs for each created agent.
Create Turtle
- Sample Netlogo Code:
crt 2
- Transformation: Changes to an optimized
_crtfast
without a command block - Performance changes: Avoids having to run separate jobs for each created agent.
Create Ordered Turtles
- Sample Netlogo Code:
cro 100
- Transformation: Changes to an optimized
_crofast
without a command block - Performance changes: Avoids having to run separate jobs for each created agent.
Patch at
- Sample Netlogo Code:
ask patch-at 1 -1 [ set pcolor green ]
- Transformation: Changes coordinate values to reference methods like
_patchnorth
,_patchsouthwest
, etc. - Performance Changes: Likely minor. Avoids certain rounding operations and may shortcut some world-wrapping.
With
- Sample Netlogo Code:
count patches with [pxcor = 5]
- Transformation: Changes
with
onpxcor
orpycor
compared for equality agains an unchanging value (constants, globals, procedure variables) to fetch a column or row of patches from the world. - Performance Changes: Potentially significant. Avoids iterating through an entire patchset and checking the
with
-predicate for every patch.
One-of with
- Sample Netlogo Code:
one-of turtles with [color = red]
- Transformation: Changes to an optimized
_oneofwith
primitive. This primitive shuffles the agentset before iterating through and returns the first matching agent that it finds. - Performance Changes: Significant. Avoids constructing an intermediate agentset and can avoid iterating through the entire agentset.
Nsum
- Sample Netlogo Code:
sum [energy] of neighbors
- Transformation: Changes sum to an optimized
_nsum
primitive which avoids shuffling the agentset and builds a sum as it goes without the need for an intermediate list. - Performance Changes: Limited. Because this only operates on eight agents (at most) the impact of reducing intermediate results is unlikely to be significant unless
sum [...] of neighbors
is heavily used in a model.
Nsum4
- Sample Netlogo Code:
sum [energy] of neighbors4
- Transformation: Changes sum to an optimized
_nsum4
primitive which avoids shuffling the agentset and builds a sum as it goes without the need for an intermediate list. - Performance Changes: Limited. Because this only operates on four agents (at most) the impact of reducing intermediate results is unlikely to be significant unless
sum [...] of neighbors
is heavily used in a model.
Count with
- Sample Netlogo Code:
count patches with [pcolor = red]
- Transformation: Changes to an optimized
_countwith
that avoids building an intermediate agentset. - Performance Changes: Can substantially improve performance due to constructing the resulting count directly without creating an intermediate agentset.
With other / Other with
- Sample Netlogo Code:
other (turtles with [color = red])
/(other turtles) with [color = red]
- Transformation: Changes to an optimized
_withother
which avoids iterating twice. - Performance Changes: Can substantially improve performance due to iterating through the agentset only once and constructing the result agentset directly instead of an intermediate agentset.
Any other
- Sample Netlogo Code:
any? other turtles-here
- Transformation: Changes to an optimized
_anyother
which performs at most one iteration through the agentset. - Performance Changes: Can substantially improve performance due to permitting only partial iteration through an agentset.
Any other with
- Sample Netlogo Code:
any? other turtles-here with [color = red]
- Transformation: Changes to an optimized
_anyotherwith
which performs a single iteration through the agentset. - Performance Changes: Can substantially improve performance due to 1) permitting only partial iteration through the agentset, 2) iterating through the agentset at most one time, 3) avoiding the overhead of constructing an intermediate agentset.
Any turtles on
- Sample Netlogo Code:
any? turtles-on patch 1 1
- Transformation: Changes to an optimized
_anyturtleson
which iterates through the agents only until a turtle is found. - Performance Changes: Can substantially improve performance due to reduced iteration through agentset.
Any breed on
- Sample NetLogo Code:
any? breeds-on patch 1 1
- Transformation: Changes to an optimized
_anybreedon
which iterates through the agents only until a turtle of breed is found. - Performance Changes: Can substantially improve performance due to reduced iteration through agentset.
Count other
- Sample Netlogo Code:
count other turtles-here
- Transformation: Changes to an optimized
_countother
which performs a single iteration through the agentset. - Performance Changes: Can substantially improve performance due to 1) iterating through the agentset only a single time, 2) avoiding the overhead of constructing an intermediate agentset.
Count other with
- Sample Netlogo Code:
count other turtles with [color = red]
- Transformation: Changes to an optimized
_countotherwith
which performs one iteration through the agentset instead of three. - Performance Changes: Can substantially improve performance due to 1) iterating through the agentset only a single time, 2) avoiding overhead of constructing an intermediate agentset.
AnyWith1:
- Sample Netlogo Code:
any? turtles with [color = red]
- Transformation: Changes to an optimized primitive
_anywith
which iterates through the agents only until a predicate match is found and returns. - Performance Changes: Can substantially improve performance due to reduced iteration through agentset.
AnyWith2:
- Sample Netlogo Code:
count turtles with [color = red] != 0
- Transformation: Changes to an
_anywith[]
. This avoids doing a full count by looking for a single agent which fulfills thewith
-predicate. - Performance Changes: Can substantially improve performance due to reduced iteration through agentset.
AnyWith3:
- Sample Netlogo Code:
count turtles with [color = red] > 0
- Optimized-equivalent Code:
any? turtles with [color = red]
- Transformation: Changes to an
_anywith[]
. This avoids doing a full count by looking for a single agent which fulfills thewith
-predicate. - Performance Changes: Can substantially improve performance due to reduced iteration through agentset.
AnyWith4:
- Sample Netlogo Code:
0 < count turtles with [color = red]
- Optimized-equivalent Code:
any? turtles with [color = red]
- Transformation: Changes to an
_anywith[]
. This avoids doing a full count by looking for a single agent which fulfills thewith
-predicate. - Performance Changes: Can substantially improve performance due to reduced iteration through agentset.
AnyWith5:
- Sample Netlogo Code:
count turtles with [color = red] = 0
- Optimized-equivalent code:
not any? turtles with [color = red]
- Transformation: Changes to a
_not[_anywith[]]
. This avoids doing a full count by looking for a single agent which fulfills thewith
-predicate. - Performance Changes: Can substantially improve performance due to reduced iteration through agentset.
Patch Variable
- Sample Netlogo Code:
ask patch [ show pxcor + pycor ]
- Transformation: Changes patch variables known to return numbers (like
pxcor
,pycor
, etc.) into_patchvariabledouble
primitives which return the patch variable as a number instead of as an object. - Performance Changes: This reduces the amount of boxing and unboxing that needs to be done when code is generated.
Turtle Variable
- Sample Netlogo Code:
ask turtles [ show xcor + ycor ]
- Transformation: Changes turtle variables known to return numbers (like
xcor
,ycor
, etc.) into_turtlevariabledouble
primitives which return the turtle variable as a number instead of as an object. - Performance Changes: This reduces the amount of boxing and unboxing that needs to be done when code is generated.
RandomConst:
- Sample Netlogo Code:
random 10
- Transformation: Changes to use an optimized
_randomconst
primitive which accepts no arguments and has the constant "baked in." - Performance Changes: Simplifies a common operation by removing the need to evaluate a separate argument.
Lost Optimizations
The following optimizations were lost between 4.0 and 4.1 when the optimizer was rewritten. Rewriting them could be a nice project someday.
Early loop exit:
_equal(_count,_constdouble:0)
=>_not(_any)
_greatherthan(_count,_constdouble:0)
=>_any
_notequal(_count,_constdouble:0)
=>_any
Avoid constructing AgentSet:
_other(_turtleshere)
=>_otherturtleshere
_other(_breedhere)
=>_otherbreedhere
_count(_turtleshere)
=>_countturtleshere
_count(_otherturtleshere)
=>_countotherturtleshere
_count(_breedhere)
=>_countbreedhere
_count(_otherbreedhere)
=>_countotherbreedhere
Generate simpler code when something about arguments is known:
_call
=>_tailcall
_callreport
=>_tailcallreport
avoid constructing list, avoid shuffling (and for some, reduce interpreter overhead):
_max/mean/min/sum(_of)
=>_max/min/mean/sumof
// Flocking, GasLab, Ising_max/mean/min/sum(_patch/turtle/breedvariableof)
=>_max/mean/min/sumpatch/turtle/breedvariableof