Improve Java Code

R

Roedy Green

u1=i+1;u2=j+1;
if ((u1<9)&&(u2<9)) {
ux=u1*3+u2;
if (Arr1[ux]=='B') {mz++;BigARRAY[mz]=ux;}
}

you could compile with Jet http://mindprod.com/jgloss/jet.html which
might version it. Presumably this code is inside a loop. One version
of the loop handles the cases where (u1<9)&&(u2<90) and one where they
are not. You effectively promote the test outside the loop by
incorporating it into the loop start/end parms. You can afford to do
quite a bit of thinking outside the loop to avoid it inside, including
having several loop.s

you could also write that as

BigARRAY[++mz]=ux;

But I doubt it would make any difference on any optimising compiler.

I would find the following code a lot easier to read..

u1 = i + 1;
u2 = j + 1;
if ( u1 < 9 && u2 < 9 )
{
ux = u1 * 3 + u2;
if ( arr1[ ux ] == 'B' )
{
mz++;
bigArray[ mz ] = ux;
}

In your version, my linear rational brain has to do the tokenising.
In mine, my parallel-processing visual cortex does it.
 
L

Lew

Roedy said:
I would find the following code a lot easier to read..

u1 = i + 1;
u2 = j + 1;
if ( u1 < 9 && u2 < 9 )
{
ux = u1 * 3 + u2;
if ( arr1[ ux ] == 'B' )
{
mz++;
bigArray[ mz ] = ux;
}

In your version, my linear rational brain has to do the tokenising.
In mine, my parallel-processing visual cortex does it.

Roedy highlights an important point - human brains and visual cortexes are
wired to do certain things very, very well, if we let them. Cognition is
slow, but deep, but slow. Visual discrimination is for practical purposes
instantaneous, as well as parallel.

Roedy's indentation makes use of the two-dimensional, instantaneous nature of
visual neural processing to convey useful information about his algorithm.
That's just good engineering - exploit the known strengths of a subsystem
guaranteed to be present in the use of the artifact.
 
D

Donkey Hot

Secondly

BigARRAY[++mz] is it same as
mz++;BigARRAY[mz]

Logically it is, and I guess performace wise too it is.
And what about BigARRAY[mz++] is it same as
mz++;BigARRAY[mz]

Absolutely not. In the first case, if mz is initially 1 then 1st case will
result to BigARRAY[1], but in 2nd case it will be BigARRAY[2]
 
L

Lew

Donkey said:
Secondly

BigARRAY[++mz] is it same as
mz++;BigARRAY[mz]

Logically it is, and I guess performace wise too it is.
And what about BigARRAY[mz++] is it same as
mz++;BigARRAY[mz]

Absolutely not. In the first case, if mz is initially 1 then 1st case will
result to BigARRAY[1], but in 2nd case it will be BigARRAY[2]

Sanny, you should review the tutorials on the Java operators. Java's pre- and
post-increment operators work the same as in C, C++ and C#.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,182
Latest member
BettinaPol

Latest Threads

Top