Difference between revisions of "Code Size/Cheat sheet"

From Robowiki
Jump to navigation Jump to search
m (Using <syntaxhighlight>.)
(do while ....)
Line 109: Line 109:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
|-
 +
| style="margin:0; background:#e0efef; border:1px solid #a3bfb1; font-weight: bold; text-align:left; color:#000; padding:0.1em 0.4em;" | Loop with while
 +
|-
 +
|style="text-align:justify"|
 +
You should always end up with less code size if you switch your for{each}( ...) 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 bound" check.
 +
 +
Do this:
 +
<syntaxhighlight>
 +
public void onScannedRobot(ScannedRobotEvent e) {
 +
    try {
 +
        Iterator<your_class> iter = yourList.iterator();
 +
while(true) {
 +
            your_class obj = iter.next();
 +
          ......  // your code
 +
        }
 +
    } catch (Exception e) {}
 +
 +
}
 +
</syntaxhighlight>
 +
 +
Don't do this: (8 byte more)
 +
<syntaxhighlight>
 +
public void onScannedRobot(ScannedRobotEvent e) {
 +
    foreach(your_class obj: yourList) {
 +
      ...... // your code
 +
    }
 +
}
 +
</syntaxhighlight>
 +
 +
 
|}
 
|}
 
| style="width: 40%; vertical-align:top;" |
 
| style="width: 40%; vertical-align:top;" |

Revision as of 16:18, 23 May 2012

This article is a stub. You can help RoboWiki by expanding it.

Code Size Cheat sheet

Do and Don't
Do Don't
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());
 }
}
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();

 }
}
A magic of register

Every operation to anything must be done from register. Java has 4 registers to hold variable. If the method is not static, the first one is this reference. The remaining registers will be use for each parameter and variable inside method. Every type of variable use 1 register, except double 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 operation, which costs codesize. This mean you should define most-used variables before any other variables.

Do this:

public void onScannedRobot(ScannedRobotEvent e) {
  int int_variable_if_you_want; 
  double bearing =  
    e.getBearingRadians() + getHadingRadians();
  double distance = e.getDistance();
  // ...
}

Don't do this:

public void onScannedRobot(ScannedRobotEvent e) {
  double distance = e.getDistance();
  int int_variable_if_you_want; 
  double bearing = 
    e.getBearingRadians() + getHadingRadians();
  // ...
}
Loop with while

You should always end up with less code size if you switch your for{each}( ...) 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 bound" check.

Do this:

public void onScannedRobot(ScannedRobotEvent e) {
    try {
        Iterator<your_class> iter = yourList.iterator();
	while(true) {
            your_class obj = iter.next();
           ......   // your code
        }	
    } catch (Exception e) {}

}

Don't do this: (8 byte more)

public void onScannedRobot(ScannedRobotEvent e) {
    foreach(your_class obj: yourList) {
       ...... // your code
    }
}


Cost
Operation Cost in bytes
creating a class 5
1st constructor free
creating a method free
returning from a method 1
calling a method 3
storing a local (non-register) variable 1
loading a local (non-register) variable 1
storing a static variable 2
loading a static variable 2
loading integer literals -1 to 5 1
loading integer literals -128 to 127 2
loading integer literals -32768 to 32767 3
loading other integer literals 2
loading double literals 0, 1 1
loading double literals -1, 2, 3, 4, 5 2
loading other double literals 3
loading string literals 2
declaring a variable free
casting 1
promotion 1
arithmetic 1
++ and -- (register or not) 1