[Home]CodeSize/WritingSmallCode

Robo Home | CodeSize | Changes | Preferences | AllPages

Difference (from prior major revision) (no other diffs)

Changed: 240c240,242
Yep, that should be the case. Though, that should be equally small as simply going "static double[] stats = new double[BINS];" during the decleration, no reason to have the seperate static block I don't think. -- Rednaxea?
Yep, that should be the case. Though, that should be equally small as simply going "static double[] stats = new double[BINS];" during the decleration, no reason to have the seperate static block I don't think. -- Rednaxela

question: why does ldc take up 1 byte less than sipush (i.e. why does loading 32768 take up one byte less than loading 32767)?

The tips on this page are specific ways you can rewrite your code to use fewer bytes, as measured by the CodeSize utility. Please note that CodeSize is not affected by the length of your method or variable names.

Discussion is at the bottom of this page.


            LinkedList list;
            try{
                someFunction(list.getFirst());
            } catch(Exception ex){}
is smaller than:
            LinkedList list;
            if(list.size() != 0){
                someFunction(list.getFirst());
            }
            _goAngle = angleFromWaveSource +
                directedGoAngle(direction = (sign(checkDanger(-1) - checkDanger(1))));
            int i = 1;
            if (j > 2) {
                i = -1;
            }
is smaller than
            int i;
            if (j > 2) {
                i = -1;
            } else {
                i = 1;
            }
            double a;
            double t = (a = e.getBearing()) * getEnergy();
is smaller than
            double a = e.getBearing();
            double t = a * getEnergy();
            StringBuilder s = new StringBuilder();
            s.append(x);
            s.append(" is my number");
            s.toString();

Please add to this table as you can, and organize it if you like:

Operation Cost in CodeSize
1st constructor free
creating a method free
returning from a method 1
calling a method 3
storing a local (non-register) variable1
loading a local (non-register) variable1
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


Discussion

What about using ternaries? So like in your example: Is

double rawr[] = new double[12];
rawr[2][4] = (rawr[].length > 0) ? 10 : rawr[2][4];
Smaller then
double rawr[] = new double[12];
if(rawr[].length > 0)
    rawr[2][4] = 10;
-- Chase-san

I would guess that they compile to the exact same thing, so there would be no gain there. -- Voidious

It is universally true (in the Java universe) that uninitialized variables are 0, false, or null. -- Simonton

First of all. Cool page, and which is a great inspiration. -- Fnl

I am wondering if:

            int i;
            if (j > 2) {
                i = -1;
            } else {
                i = 1;
            }
Could be written even smaller:
            int i = (j > 2) ? -1 : 1;
At least it should have a faster execution time! ;-) --Fnl

Hehe..

            LinkedList list;
            try{
                someFunction(list.getFirst());
            } catch(Exception ex){}
..is smaller, but more expensive in execution time due to the try-catch! ;-) --Fnl

A few of my tricks. Remember your binary operators &, |, and especially ^ (xor). Xor is great for swapping very cheaply between 2 integer numbers for next to nothing:

            variable = 64;
...
            variable ^= 32;
is a very cheap way to swap between 64 and 96. Just be careful of the binary math :)

It is almost always cheaper to directly call a method that has 2 or more lines of code than to repeat the code. I've manually called OnDeath(null) before to reinitialize variables and such.

In the spirit of smaller numbers, it is cheaper to divide by 5 than to multiply my 0.2. Same for any other operator.

I know there is another page around here I've posted on before about this - one I started a long time ago. I also know there is a lost thread over at the Repository than had similiar info. I wonder if that could be recovered. --Miked0801

Well, here's one little trick I found that I thought I'd share. At least with Java 6 compiling in Java 5 compatibility mode (I haven't tested other configurations), the following:

public class Foo {
    public Foo() {}

    public static final Foo getOne() {
        return new Foo();
    }
}

def void doStuff() {
    Foo a = Foo.getOne();
    Foo b = Foo.getOne();
    Foo c = Foo.getOne();
}
is smaller than:
public class Foo {
    public Foo() {}
}

def void doStuff() {
    Foo a = new Foo();
    Foo b = new Foo();
    Foo c = new Foo();
}
In other words it appears that factory methods are smaller than constructors if used 3 or more times. They are the same size if used twice, but are larger if the constructor is only used once. -- Rednaxela

Lol, double bulletPower = 3/2; uses up less space than double bulletPower = 1.5; (2 bytes(?) to be exact) --Starrynte

That would be because 3/2 is integer arithmetic.... essentially equivalent to 1. Try putting 1.0 in there, it should be the same =) -- Skilgannon

In a method,

if(condition){
   foo1();
   return;
}
foo2();
is 2 bytes smaller than
if(condition){
    foo1();
}else{
    foo2();
}
--Starrynte

I haven't tested this, but I believe that

static {
    stats=new double[BINS];
}
is smaller than
public void run(){
    if(stats==null) stats=new double[BINS]
    .
    .
    .
}
(of course if the stats reset after every round then it doesn't work) --Starrynte

Yep, that should be the case. Though, that should be equally small as simply going "static double[] stats = new double[BINS];" during the decleration, no reason to have the seperate static block I don't think. -- Rednaxela

question: why does ldc take up 1 byte less than sipush (i.e. why does loading 32768 take up one byte less than loading 32767)?


Robo Home | CodeSize | Changes | Preferences | AllPages
Edit text of this page | View other revisions
Last edited April 2, 2009 3:47 EST by cpe-75-84-83-181.socal.res.rr.com (diff)
Search: