Getting rid of "if condition" by some math equation.

R

RedGrittyBrick

it is helpful to make your code as ordinary as possible.

That's always been my rule of thumb. Assume compiler writers and chip
designers are smarter than I am and write the sort of code I would
expect them to expect.
 
J

Jim Janney

Sanny said:
I use String comparision and if "str.charat() is not '-'"

We add bit value to xx.

The below code takes long time as we need to do 50 "if" comparisions
each time the function is called.

Below is the sample Java Code:

================================

long xx=0; long bit=1;

if (Str.charAt(50)!='-') xx+=bit;bit++;
if (Str.charAt(49)!='-') xx+=bit;bit++;
if (Str.charAt(48)!='-') xx+=bit;bit++;

[snipped for tedium]
if (Str.charAt(3)!='-') xx+=bit;bit++;
if (Str.charAt(2)!='-') xx+=bit;bit++;
if (Str.charAt(1)!='-') xx+=bit;bit++;
if (Str.charAt(0)!='-') xx+=bit;bit++;


================================

Since most of the time taken is by the "if condition."

I am thinking of using

char mm='-';
int mm1=Character.getNumericValue(mm);

int mm2=Str.charAt(0);

int mm3=mm2-mm1;

Now if I subtract mm2-mm1=mm3 Then I have to have xx+=bit; only when
mm3==0;

I want to write an Math Equation that computes mm3 and when mm3 value
is "0" xx+=bit; is done.

I am not getting the clue how to develop this equation to get rid of
the "If conditions"

Please suggest some equation or any other way to do this in one
statement.

ENUMERATION? can these be used here to solve these "if conditions" in
one step?

Bye
Sanny

As long as you're unrolling the loop and using constant indexes, you
might as well go all the way:

long xx=0;
char[] s = Str.toCharArray();

if (s[50]!='-') xx+=1;
if (s[49]!='-') xx+=2;
if (s[48]!='-') xx+=3;
if (s[47]!='-') xx+=4;
// etc...
 
A

Arne Vajhøj

Just try the above for loops and you will see how much time spent in
both cases.

You have to put extra efforts to create a timer.

Your problem => you do the work !
I tested these an year back. and found if conditions are taking a lot
of time than other arithmetic problems.

There are always somebody that have an aunt that had a neighbor
that at some point in time may have heard from someone that <whatever>.

Arne
 
M

markspace

RedGrittyBrick said:
That's always been my rule of thumb. Assume compiler writers and chip
designers are smarter than I am and write the sort of code I would
expect them to expect.


This.

Make the code correct first, including correct OO design and code
structure. Then measure, and optimize as needed.
 
R

Roedy Green

if (Str.charAt(50)!='-') xx+=bit;bit++;
if (Str.charAt(49)!='-') xx+=bit;bit++;

This code does not make sense. Why would you want the sum of the bit
numbers that contained a -? I could see xx++ to count the number of -
chars.

Very rarely do you manually unravel loops now adays. We leave it up
to optimisers which loops to unravel.

That is confusing code. A novice might read it as
if (Str.charAt(50)!='-') {xx+=bit;bit++;}
Visual form should reflect logic. This is a form of obfuscated code.
 
J

Jim Janney

Jim Janney said:
Sanny said:
I use String comparision and if "str.charat() is not '-'"

We add bit value to xx.

The below code takes long time as we need to do 50 "if" comparisions
each time the function is called.

Below is the sample Java Code:

================================

long xx=0; long bit=1;

if (Str.charAt(50)!='-') xx+=bit;bit++;
if (Str.charAt(49)!='-') xx+=bit;bit++;
if (Str.charAt(48)!='-') xx+=bit;bit++;

[snipped for tedium]
if (Str.charAt(3)!='-') xx+=bit;bit++;
if (Str.charAt(2)!='-') xx+=bit;bit++;
if (Str.charAt(1)!='-') xx+=bit;bit++;
if (Str.charAt(0)!='-') xx+=bit;bit++;


================================

Since most of the time taken is by the "if condition."

I am thinking of using

char mm='-';
int mm1=Character.getNumericValue(mm);

int mm2=Str.charAt(0);

int mm3=mm2-mm1;

Now if I subtract mm2-mm1=mm3 Then I have to have xx+=bit; only when
mm3==0;

I want to write an Math Equation that computes mm3 and when mm3 value
is "0" xx+=bit; is done.

I am not getting the clue how to develop this equation to get rid of
the "If conditions"

Please suggest some equation or any other way to do this in one
statement.

ENUMERATION? can these be used here to solve these "if conditions" in
one step?

Bye
Sanny

As long as you're unrolling the loop and using constant indexes, you
might as well go all the way:

long xx=0;
char[] s = Str.toCharArray();

if (s[50]!='-') xx+=1;
if (s[49]!='-') xx+=2;
if (s[48]!='-') xx+=3;
if (s[47]!='-') xx+=4;
// etc...

long xx = 50 * 51 / 2; // sum of all positions
int i = Str.indexOf('-');
while (i != -1) {
xx -= 51 - i; // subtract the dashed positions
i = Str.indexOf('-', i+1);
}

Still not sure why anyone would want to do this, but presumably it
makes sense in the right context.
 
J

Jim Janney

Peter Duniho said:
Jim said:
[...]
As long as you're unrolling the loop and using constant indexes, you
might as well go all the way:

long xx=0;
char[] s = Str.toCharArray();

if (s[50]!='-') xx+=1;
if (s[49]!='-') xx+=2;
if (s[48]!='-') xx+=3;
if (s[47]!='-') xx+=4;
// etc...

long xx = 50 * 51 / 2; // sum of all positions
int i = Str.indexOf('-');
while (i != -1) {
xx -= 51 - i; // subtract the dashed positions
i = Str.indexOf('-', i+1);
}

Hmm. Your first suggestion, I get. If the OP thinks that it's really
worth unrolling the loop, they certainly also should believe that
embedding known constants into the logic is also worthwhile.

But your second proposal? You still have a loop (which the OP
superstitiously wishes to avoid), _and_ you've introduced additional
overhead in the call to indexOf() (which is likely to be more costly
than charAt(), since it embeds a new loop inside your outer one…even
though you still essentially visit each character once, in order, two
loops are harder for the JIT compiler and CPU to deal with than one).

And it's much harder to read as well, as compared to the simple,
unoptimized loop I proposed early on. How is it better to initialize
an accumulator to the sum of the series, and then subtract hyphen
positions, as opposed to initializing the accumulator to 0 and just
adding the other positions as they are found? (If we had some prior
knowledge that there are always many fewer hyphens than other
characters in the string, I could see a theoretical, though still
impractical, benefit…but that's not part of the problem statement).

The best advice the OP has received so far remains: start with
correct, well-designed code, then measure and optimize only as needed
and demonstrably effective to meet specific, unambiguous performance
criteria.

(It was also pointed out that the compiler and CPU designers should be
trusted first. To some extent it's not even that they are so much
smarter and better at optimizations, though that is actually very
likely in most cases. It's that the compiler and CPU _do_
optimizations, those optimizations generally assume certain commonly
used patterns, and writing code that attempts to optimize based on
assumptions rather than real-world measurements are likely to thwart
the ability of the compiler and CPU to optimize, by failing to use
those known, commonly used patterns. "Optimizing" outside the context
of actual measurements can be, and often is, counter-productive).

Agreed. It simply occurred to me, looking at the version with
imbedded constants, that there was another way to approach the
problem, and I was curious to see what it would look like. And having
coded it, I naturally had to post it...
 

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,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top