Difference between revisions of "Code Size/Cheat sheet"

From Robowiki
Jump to navigation Jump to search
(cheaet-sheet for small codesize; still stubs)
 
m (Redirected page to Codesize#Common Tricks)
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{stub}}
+
#REDIRECT [[Codesize#Common_Tricks]]
 
 
<div style="background-color:#060; font-size:1px; height:8px; border:1px solid #AAAAAA; -moz-border-radius-topright:0.5em; -moz-border-radius-topleft:0.5em;"></div>
 
<div style="background-color:#FFFFFF;border:1px solid #AAAAAA; border-top:0px solid white; padding:5px 5px 0 5px; margin-bottom:3ex">
 
<center>
 
<big style="color: #000; display: block; background: #cfc; border:solid 1px #060;padding: 0.3em;">'''Code Size Cheat sheet'''</big>
 
 
 
{| style="width: 100%; vertical-align:top;"
 
| style="width: 60%; vertical-align:top; border-right: 1px solid #aaa" |
 
{| width="100%" cellpadding="2" cellspacing="5" style="vertical-align:top;"
 
| style="margin:0; background:#e0efef; border:1px solid #a3bfb1; font-weight: bold; text-align:left; color:#000; padding:0.1em 0.4em;" | Do and Don't
 
|-
 
|
 
{| width="100%"
 
|style="text-align:center;font-weight:bold;width:50%"| Do ||style="text-align:center;font-weight:bold;width:50%"| Don't
 
|-
 
|style="padding: 0"|
 
<pre>
 
class Robot
 
  extends AdvancedRobot
 
{
 
static double varname;
 
public void run() {
 
 
 
  LinkedList list;
 
  try{
 
  someFunction
 
    (list.getFirst());
 
  } catch(Exception ex){}
 
 
 
  int i = 1;
 
  if (j > 2) {
 
  i = -1;
 
  }
 
 
 
 
 
}
 
 
 
public void
 
    onScannedRobot
 
    (ScannedRobotEvent e)
 
  double a;
 
  double t = getEnergy()
 
    * (a = e.getBearing());
 
}
 
}
 
</pre>
 
|
 
<pre>
 
class Robot
 
  extends AdvancedRobot
 
{
 
double varname = 0;
 
public void run() {
 
 
 
  LinkedList list;
 
  if(list.size() != 0){
 
  someFunction
 
    (list.getFirst());
 
  }
 
 
 
  int i;
 
  if (j > 2) {
 
  i = -1;
 
  } else {
 
  i = 1;
 
  }
 
}
 
 
 
public void
 
    onScannedRobot
 
    (ScannedRobotEvent e)
 
  double a = e.getBearing();
 
  double t = a * getEnergy();
 
 
 
}
 
}
 
</pre>
 
<!--
 
The code is very ugly, agree?
 
 
 
Because I can't fit it in remaining 30% of full table. Please help me manage this page if you can.
 
-->
 
|}
 
|-
 
| style="margin:0; background:#e0efef; border:1px solid #a3bfb1; font-weight: bold; text-align:left; color:#000; padding:0.1em 0.4em;" | A magic of register
 
|-
 
|style="text-align:justisfy"|
 
Every operation to anything must be done from register. Java has 4 registers to hold variable. The first one is <code>this</code> reference. The remaining register will be use for each parameter and variable inside method. Every type of variable use 1 register, except double use 2. In case of double come when 3 registers is already use by other variable, that double value will be place on an extra register. If you do any operation on any variable that do not on the register, Java will need to move that variable to the register before doing any of your operation, which cost codesize. This mean you should define a most-use variable before any other variables.
 
 
 
Do this:
 
<pre>
 
public void onScannedRobot(ScannedRobotEvent e) {
 
  int int_variable_if_you_want;
 
  double bearing = 
 
    e.getBearingRadians() + getHadingRadians();
 
  double distance = e.getDistance();
 
  // ...
 
}
 
</pre>
 
Don't do this:
 
<pre>
 
public void onScannedRobot(ScannedRobotEvent e) {
 
  double distance = e.getDistance();
 
  int int_variable_if_you_want;
 
  double bearing =
 
    e.getBearingRadians() + getHadingRadians();
 
  // ...
 
}
 
</pre>
 
|}
 
| style="width: 40%; vertical-align:top;" |
 
{| cellpadding="2" cellspacing="5" style="width: 100% vertical-align:top;"
 
| style="margin:0; background:#e0efef; border:1px solid #a3bfb1; font-weight: bold; text-align:left; color:#000; padding:0.1em 0.4em;" | Cost
 
|-
 
|
 
{|
 
| '''Operation'''                        ||style="text-align:right"| '''Cost in bytes'''
 
|-
 
| creating a class                        ||style="text-align:right"| 5
 
|-
 
| 1st constructor                        ||style="text-align:right"| free
 
|-
 
| creating a method                      ||style="text-align:right"| free
 
|-
 
| returning from a method                ||style="text-align:right"| 1
 
|-
 
| calling a method                        ||style="text-align:right"| 3
 
|-
 
| storing a local (non-register) variable ||style="text-align:right"| 1
 
|-
 
| loading a local (non-register) variable ||style="text-align:right"| 1
 
|-
 
| storing a static variable              ||style="text-align:right"| 2
 
|-
 
| loading a static variable              ||style="text-align:right"| 2
 
|-
 
| loading integer literals -1 to 5        ||style="text-align:right"| 1
 
|-
 
| loading integer literals -128 to 127    ||style="text-align:right"| 2
 
|-
 
| loading integer literals -32768 to 32767 ||style="text-align:right"| 3
 
|-
 
| loading other integer literals          ||style="text-align:right"| 2
 
|-
 
| loading double literals 0, 1            ||style="text-align:right"| 1
 
|-
 
| loading double literals -1, 2, 3, 4, 5  ||style="text-align:right"| 2
 
|-
 
| loading other double literals          ||style="text-align:right"| 3
 
|-
 
| loading string literals                ||style="text-align:right"| 2
 
|-
 
| declaring a variable                    ||style="text-align:right"| free
 
|-
 
| casting                                ||style="text-align:right"| 1
 
|-
 
| promotion                              ||style="text-align:right"| 1
 
|-
 
| arithmetic                              ||style="text-align:right"| 1
 
|-
 
| ++ and -- (register or not)            ||style="text-align:right"| 1
 
|}
 
|}
 
|}
 
 
 
</center>
 
</div>
 

Latest revision as of 14:45, 5 March 2013