-
-
Notifications
You must be signed in to change notification settings - Fork 239
Optimization List
Jeremy B edited this page Apr 2, 2024
·
4 revisions
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.
- 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
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
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
- Extensions
- NetLogo Libraries
- Controlling API
- Extensions API
- 6.0 Extension and Controlling API Transition-Guide
- Optimizing NetLogo Runs
- Setting English as the Default Language when Running NetLogo
- Unofficial Features
- Advanced Installation
- Optimization List
- Java System Properties and how to use them
- NetLogo on ARM Devices
- Multiple Views via HubNet
- Branches
- Building
- Tests
- Windows Setup
- Continuous Integration
- Draft: How to Help
- Google Summer of Code Ideas List
- Syntax Highlighting
- Building with IntelliJ
- Code Formatting
- Localization
- File (.nlogo) and Widget Format
- Benchmarking
- Releasing
- Preparing the Models Library for Release
- Documentation
- NetLogo Bundled Java Versions
- JOGL
- Plugins API
- Architecture
- LazyAgentset
- Model Runs and Review Tab
- Model Runs: To Do and Code Overview
- Notes on in Radius
- Archived Branches
- The nlogox format
- Touch API Proposal
- Why isn't NetLogo "Parallel"?
- Potential Speedups
- Tortoise
- SimServer, WebStart, and NetLogo in Classrooms