Difference between revisions of "Code Size"
(→Common Tricks: Java has no foreach structure, it's just `for` with a colon instead of semicolons) |
(Remove empty section) |
||
(3 intermediate revisions by 2 users not shown) | |||
Line 90: | Line 90: | ||
|- | |- | ||
− | | style="margin:0; background:#e0efef; border:1px solid #a3bfb1; font-weight: bold; text-align:left; color:#000; padding:0.1em 0.4em;" | Declare variables on | + | | style="margin:0; background:#e0efef; border:1px solid #a3bfb1; font-weight: bold; text-align:left; color:#000; padding:0.1em 0.4em;" | Declare variables on their first use |
|- | |- | ||
|style="text-align:justify"| | |style="text-align:justify"| | ||
Line 105: | Line 105: | ||
|- | |- | ||
− | | style="margin:0; background:#e0efef; border:1px solid #a3bfb1; font-weight: bold; text-align:left; color:#000; padding:0.1em 0.4em;" | | + | | style="margin:0; background:#e0efef; border:1px solid #a3bfb1; font-weight: bold; text-align:left; color:#000; padding:0.1em 0.4em;" | The magic of registers |
|- | |- | ||
|style="text-align:justify"| | |style="text-align:justify"| | ||
− | Every operation | + | Every operation must be done from registers. Java has 4 registers to hold variables. If the method is not static, the first one is a <code>this</code> reference. The remaining registers will then be used for each parameter and variable inside the method. Every type of variable uses 1 register, except doubles, which use 2. When 3 registers are already used by other variable, a double value will then be assigned an extra register (and the codesize is as if there was only 1 used). If you do any operation on any variable that is not on the register, Java will need to move that variable to the register before doing any of your operations, which increases codesize. This means you should define most-used variables before any other variables. |
Do this: | Do this: | ||
Line 115: | Line 115: | ||
int int_variable_if_you_want; | int int_variable_if_you_want; | ||
double bearing = | double bearing = | ||
− | e.getBearingRadians() + | + | e.getBearingRadians() + getHeadingRadians(); |
double distance = e.getDistance(); | double distance = e.getDistance(); | ||
// ... | // ... | ||
Line 126: | Line 126: | ||
int int_variable_if_you_want; | int int_variable_if_you_want; | ||
double bearing = | double bearing = | ||
− | e.getBearingRadians() + | + | e.getBearingRadians() + getHeadingRadians(); |
// ... | // ... | ||
} | } | ||
Line 135: | Line 135: | ||
|- | |- | ||
|style="text-align:justify"| | |style="text-align:justify"| | ||
− | You should always end up with less code size if you switch your for | + | You should always end up with less code size if you switch your for( ...) loops to while loops. This applies to Lists as well to Arrays, just put an try..catch.. around the while to save the null, zero or "array out of bounds" check. |
Do this: | Do this: | ||
Line 151: | Line 151: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | Don't do this: (8 | + | Don't do this: (8 bytes more) |
<syntaxhighlight> | <syntaxhighlight> | ||
public void onScannedRobot(ScannedRobotEvent e) { | public void onScannedRobot(ScannedRobotEvent e) { | ||
Line 160: | Line 160: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |- | ||
+ | | style="margin:0; background:#e0efef; border:1px solid #a3bfb1; font-weight: bold; text-align:left; color:#000; padding:0.1em 0.4em;" | Use onStatus to turn [[Radar]] | ||
+ | |- | ||
+ | |style="text-align:justify"| | ||
+ | Do this: | ||
+ | <syntaxhighlight> | ||
+ | public void onStatus( StatusEvent e) { | ||
+ | setTurnRadarRightRadians(1); | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Don't do this (1 byte more): | ||
+ | <syntaxhighlight> | ||
+ | public void run() { | ||
+ | do { | ||
+ | turnRadarRightRadians(1); | ||
+ | } while (true); | ||
+ | } | ||
+ | </syntaxhighlight> | ||
|} | |} | ||
Line 225: | Line 244: | ||
== See Also == | == See Also == | ||
− | * | + | * Check the code size [[Talk:Code_Size|Discussion]] page and ask your question |
− | * | + | * Many [[NanoBots]] and [[MicroBots]] are open source and can be studied to learn more about code size reduction. |
* [[CodeSize/Old discussion]] | * [[CodeSize/Old discussion]] | ||
− | |||
− | |||
− | |||
[[Category:Terminology|Code Size]] | [[Category:Terminology|Code Size]] |
Latest revision as of 03:02, 13 August 2017
- Code Size redirects here, for other uses see Code Size (disambiguation)
- Byte redirects here, for other uses see Byte (disambiguation)
General
It represents the amount of executing code in a .class file or .jar. Code size is important to you if you want to make Robots for the smaller weight classes. It can be challenging, educational, frustrating or all at once to reduce the code to the targeted level. It is not unusual that you spend hours just to find one measly byte or have to reshuffle your ideas because they won't fit the restricted weight class. This page gives you an overview of how to reduce your code, the utilities to measure the size, the weight classes and some common tricks.
How to measure
The Robocode client is one option to measure the size of your robot. You can do this if you "Package robot for upload" from the "Robots" menu. If you packed your robot successfully the client will show you the size with a little message window.
Another option to make the code size measuring a little more handy, is to use the Code Size Utility straight within your eclipse configuration or from the console.
Weight Classes
Weight classes apply for 1v1 and for melee rumble the same.
Class Code Size General no restrictions MiniBots 1499 bytes or less MicroBots 749 bytes or less NanoBots 249 bytes or less Twin Duel 1999 bytes or less ( this means two instances of one bot 1999 bytes, or instance of bot one + instance of bot two = 1999 bytes) Teams no restrictions
Common Tricks
Code Size Cheat sheet
|
|
See Also
- Check the code size Discussion page and ask your question
- Many NanoBots and MicroBots are open source and can be studied to learn more about code size reduction.
- CodeSize/Old discussion