Ulf Zibis Ulf.Zibis at gmx.de
Wed Aug 26 08:04:07 PDT 2009

```Hi Christian,

I tried a test (see below).

Now I'm wondering, that loop1 is slightly faster than loop2, as ...
loop1 has:
3. store indirected 2nd constant
loop2 has:
1. increment offset
2. store indirected 2nd constant

Do you know any reason for this ?

/**
*
* @author Ulf Zibis <Ulf.Zibis at CoSoCo.de>
*/
public class IncRandomSpeed {

static void loop1(int off, char in1, char in2, char[] out) {
out[off+3] = in1;
out[off+5] = in2;
out[off+0] = in1;
out[off+4] = in2;
out[off+9] = in1;
out[off+8] = in2;
out[off+6] = in1;
out[off+1] = in2;
out[off+7] = in1;
out[off+2] = in2;
}

static void loop2(int off, char in1, char in2, char[] out) {
out[off++] = in1;
out[off++] = in2;
out[off++] = in1;
out[off++] = in2;
out[off++] = in1;
out[off++] = in2;
out[off++] = in1;
out[off++] = in2;
out[off++] = in1;
out[off++] = in2;
}

static final int SIZE = 1000;
static char[] out = new char[SIZE+10];

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
long time = System.nanoTime();
long time1 = 0;
long time2 = 0;
for (char i=0; i<100; i++)
for (char j=0; j<1000; j++) {
time = System.nanoTime();
for (int k=0; k<SIZE; k++)
loop1(k, i, j, out);
time1 += System.nanoTime() - time;
time = System.nanoTime();
for (int k=0; k<SIZE; k++)
loop2(k, i, j, out);
time2 += System.nanoTime() - time;
}
System.out.println("time1: "+time1);
System.out.println("time2: "+time2);
}

}

Am 24.08.2009 17:53, Christian Thalinger schrieb:
>
> Well, this was only pseudo-code, as was yours.  Let's get a little deeper...
>
> base+index is calculated only once into a temporary register and used
> afterwards.  But if that happens depends on the register allocator and
> register pressure.
>

Hm, that's what I thought as first, so in forward case cpu should do:
1. base+index -> index register
2. move highSurrogate(accumulator), [index register]
3. increment index register
4. move lowSurrogate(accumulator), [index register]

Backward case cpu should do:
1. base+index -> index register
2. move -> temp
3. increment index register
4. move lowSurrogate(accumulator), [index register]
5. load temp -> index register
6. move highSurrogate(accumulator), [index register]

Please excuse, that I'm insisting, but I really don't understand why
both should run in same time.
Can you explain once more?

-Ulf

>
>>  > a register machine here, not a stack machine
>> I'm too wondering, how method parameters were passed by register,
>> especially if they were more.
>>
>
> On IA32?  The first two integer arguments are passed in registers, the
> rest on the stack, on SPARC it's different.  But again, we are only
> talking in pseudo-code here.
>

Yes, of course.

```