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

S

Sanny

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++;
if (Str.charAt(47)!='-') xx+=bit;bit++;
if (Str.charAt(46)!='-') xx+=bit;bit++;
if (Str.charAt(45)!='-') xx+=bit;bit++;
if (Str.charAt(44)!='-') xx+=bit;bit++;
if (Str.charAt(43)!='-') xx+=bit;bit++;
if (Str.charAt(42)!='-') xx+=bit;bit++;
if (Str.charAt(41)!='-') xx+=bit;bit++;
if (Str.charAt(40)!='-') xx+=bit;bit++;
if (Str.charAt(39)!='-') xx+=bit;bit++;
if (Str.charAt(38)!='-') xx+=bit;bit++;
if (Str.charAt(37)!='-') xx+=bit;bit++;
if (Str.charAt(36)!='-') xx+=bit;bit++;
if (Str.charAt(35)!='-') xx+=bit;bit++;
if (Str.charAt(34)!='-') xx+=bit;bit++;
if (Str.charAt(33)!='-') xx+=bit;bit++;
if (Str.charAt(32)!='-') xx+=bit;bit++;
if (Str.charAt(31)!='-') xx+=bit;bit++;
if (Str.charAt(30)!='-') xx+=bit;bit++;
if (Str.charAt(29)!='-') xx+=bit;bit++;
if (Str.charAt(28)!='-') xx+=bit;bit++;
if (Str.charAt(27)!='-') xx+=bit;bit++;
if (Str.charAt(26)!='-') xx+=bit;bit++;
if (Str.charAt(25)!='-') xx+=bit;bit++;
if (Str.charAt(24)!='-') xx+=bit;bit++;
if (Str.charAt(23)!='-') xx+=bit;bit++;
if (Str.charAt(22)!='-') xx+=bit;bit++;
if (Str.charAt(21)!='-') xx+=bit;bit++;
if (Str.charAt(20)!='-') xx+=bit;bit++;
if (Str.charAt(19)!='-') xx+=bit;bit++;
if (Str.charAt(18)!='-') xx+=bit;bit++;
if (Str.charAt(17)!='-') xx+=bit;bit++;
if (Str.charAt(16)!='-') xx+=bit;bit++;
if (Str.charAt(15)!='-') xx+=bit;bit++;
if (Str.charAt(14)!='-') xx+=bit;bit++;
if (Str.charAt(13)!='-') xx+=bit;bit++;
if (Str.charAt(12)!='-') xx+=bit;bit++;
if (Str.charAt(11)!='-') xx+=bit;bit++;
if (Str.charAt(10)!='-') xx+=bit;bit++;
if (Str.charAt(9)!='-') xx+=bit;bit++;
if (Str.charAt(8)!='-') xx+=bit;bit++;
if (Str.charAt(7)!='-') xx+=bit;bit++;
if (Str.charAt(6)!='-') xx+=bit;bit++;
if (Str.charAt(5)!='-') xx+=bit;bit++;
if (Str.charAt(4)!='-') xx+=bit;bit++;
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
 
K

Knute Johnson

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++;
if (Str.charAt(47)!='-') xx+=bit;bit++;
if (Str.charAt(46)!='-') xx+=bit;bit++;
if (Str.charAt(45)!='-') xx+=bit;bit++;
if (Str.charAt(44)!='-') xx+=bit;bit++;
if (Str.charAt(43)!='-') xx+=bit;bit++;
if (Str.charAt(42)!='-') xx+=bit;bit++;
if (Str.charAt(41)!='-') xx+=bit;bit++;
if (Str.charAt(40)!='-') xx+=bit;bit++;
if (Str.charAt(39)!='-') xx+=bit;bit++;
if (Str.charAt(38)!='-') xx+=bit;bit++;
if (Str.charAt(37)!='-') xx+=bit;bit++;
if (Str.charAt(36)!='-') xx+=bit;bit++;
if (Str.charAt(35)!='-') xx+=bit;bit++;
if (Str.charAt(34)!='-') xx+=bit;bit++;
if (Str.charAt(33)!='-') xx+=bit;bit++;
if (Str.charAt(32)!='-') xx+=bit;bit++;
if (Str.charAt(31)!='-') xx+=bit;bit++;
if (Str.charAt(30)!='-') xx+=bit;bit++;
if (Str.charAt(29)!='-') xx+=bit;bit++;
if (Str.charAt(28)!='-') xx+=bit;bit++;
if (Str.charAt(27)!='-') xx+=bit;bit++;
if (Str.charAt(26)!='-') xx+=bit;bit++;
if (Str.charAt(25)!='-') xx+=bit;bit++;
if (Str.charAt(24)!='-') xx+=bit;bit++;
if (Str.charAt(23)!='-') xx+=bit;bit++;
if (Str.charAt(22)!='-') xx+=bit;bit++;
if (Str.charAt(21)!='-') xx+=bit;bit++;
if (Str.charAt(20)!='-') xx+=bit;bit++;
if (Str.charAt(19)!='-') xx+=bit;bit++;
if (Str.charAt(18)!='-') xx+=bit;bit++;
if (Str.charAt(17)!='-') xx+=bit;bit++;
if (Str.charAt(16)!='-') xx+=bit;bit++;
if (Str.charAt(15)!='-') xx+=bit;bit++;
if (Str.charAt(14)!='-') xx+=bit;bit++;
if (Str.charAt(13)!='-') xx+=bit;bit++;
if (Str.charAt(12)!='-') xx+=bit;bit++;
if (Str.charAt(11)!='-') xx+=bit;bit++;
if (Str.charAt(10)!='-') xx+=bit;bit++;
if (Str.charAt(9)!='-') xx+=bit;bit++;
if (Str.charAt(8)!='-') xx+=bit;bit++;
if (Str.charAt(7)!='-') xx+=bit;bit++;
if (Str.charAt(6)!='-') xx+=bit;bit++;
if (Str.charAt(5)!='-') xx+=bit;bit++;
if (Str.charAt(4)!='-') xx+=bit;bit++;
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

public class test {
public static void main(String[] args) {
String str = "lkee-sli2<F2-8s0--slk3vUIGN_-";
str = str.replaceAll("-","");
int len = str.length();
long xx = len * (len + 1) / 2;
System.out.println(xx);
}
}
 
K

Knute Johnson

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++;
if (Str.charAt(47)!='-') xx+=bit;bit++;
if (Str.charAt(46)!='-') xx+=bit;bit++;
if (Str.charAt(45)!='-') xx+=bit;bit++;
if (Str.charAt(44)!='-') xx+=bit;bit++;
if (Str.charAt(43)!='-') xx+=bit;bit++;
if (Str.charAt(42)!='-') xx+=bit;bit++;
if (Str.charAt(41)!='-') xx+=bit;bit++;
if (Str.charAt(40)!='-') xx+=bit;bit++;
if (Str.charAt(39)!='-') xx+=bit;bit++;
if (Str.charAt(38)!='-') xx+=bit;bit++;
if (Str.charAt(37)!='-') xx+=bit;bit++;
if (Str.charAt(36)!='-') xx+=bit;bit++;
if (Str.charAt(35)!='-') xx+=bit;bit++;
if (Str.charAt(34)!='-') xx+=bit;bit++;
if (Str.charAt(33)!='-') xx+=bit;bit++;
if (Str.charAt(32)!='-') xx+=bit;bit++;
if (Str.charAt(31)!='-') xx+=bit;bit++;
if (Str.charAt(30)!='-') xx+=bit;bit++;
if (Str.charAt(29)!='-') xx+=bit;bit++;
if (Str.charAt(28)!='-') xx+=bit;bit++;
if (Str.charAt(27)!='-') xx+=bit;bit++;
if (Str.charAt(26)!='-') xx+=bit;bit++;
if (Str.charAt(25)!='-') xx+=bit;bit++;
if (Str.charAt(24)!='-') xx+=bit;bit++;
if (Str.charAt(23)!='-') xx+=bit;bit++;
if (Str.charAt(22)!='-') xx+=bit;bit++;
if (Str.charAt(21)!='-') xx+=bit;bit++;
if (Str.charAt(20)!='-') xx+=bit;bit++;
if (Str.charAt(19)!='-') xx+=bit;bit++;
if (Str.charAt(18)!='-') xx+=bit;bit++;
if (Str.charAt(17)!='-') xx+=bit;bit++;
if (Str.charAt(16)!='-') xx+=bit;bit++;
if (Str.charAt(15)!='-') xx+=bit;bit++;
if (Str.charAt(14)!='-') xx+=bit;bit++;
if (Str.charAt(13)!='-') xx+=bit;bit++;
if (Str.charAt(12)!='-') xx+=bit;bit++;
if (Str.charAt(11)!='-') xx+=bit;bit++;
if (Str.charAt(10)!='-') xx+=bit;bit++;
if (Str.charAt(9)!='-') xx+=bit;bit++;
if (Str.charAt(8)!='-') xx+=bit;bit++;
if (Str.charAt(7)!='-') xx+=bit;bit++;
if (Str.charAt(6)!='-') xx+=bit;bit++;
if (Str.charAt(5)!='-') xx+=bit;bit++;
if (Str.charAt(4)!='-') xx+=bit;bit++;
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

public class test {
public static void main(String[] args) {
String str = "lkee-sli2<F2-8s0--slk3vUIGN_-";
str = str.replaceAll("-","");
int len = str.length();
long xx = len * (len + 1) / 2;
System.out.println(xx);
}
}
 
S

Sanny

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++;
  if (Str.charAt(47)!='-') xx+=bit;bit++;
  if (Str.charAt(46)!='-') xx+=bit;bit++;
  if (Str.charAt(45)!='-') xx+=bit;bit++;
  if (Str.charAt(44)!='-') xx+=bit;bit++;
  if (Str.charAt(43)!='-') xx+=bit;bit++;
  if (Str.charAt(42)!='-') xx+=bit;bit++;
  if (Str.charAt(41)!='-') xx+=bit;bit++;
  if (Str.charAt(40)!='-') xx+=bit;bit++;
  if (Str.charAt(39)!='-') xx+=bit;bit++;
  if (Str.charAt(38)!='-') xx+=bit;bit++;
  if (Str.charAt(37)!='-') xx+=bit;bit++;
  if (Str.charAt(36)!='-') xx+=bit;bit++;
  if (Str.charAt(35)!='-') xx+=bit;bit++;
  if (Str.charAt(34)!='-') xx+=bit;bit++;
  if (Str.charAt(33)!='-') xx+=bit;bit++;
  if (Str.charAt(32)!='-') xx+=bit;bit++;
  if (Str.charAt(31)!='-') xx+=bit;bit++;
  if (Str.charAt(30)!='-') xx+=bit;bit++;
  if (Str.charAt(29)!='-') xx+=bit;bit++;
  if (Str.charAt(28)!='-') xx+=bit;bit++;
  if (Str.charAt(27)!='-') xx+=bit;bit++;
  if (Str.charAt(26)!='-') xx+=bit;bit++;
  if (Str.charAt(25)!='-') xx+=bit;bit++;
  if (Str.charAt(24)!='-') xx+=bit;bit++;
  if (Str.charAt(23)!='-') xx+=bit;bit++;
  if (Str.charAt(22)!='-') xx+=bit;bit++;
  if (Str.charAt(21)!='-') xx+=bit;bit++;
  if (Str.charAt(20)!='-') xx+=bit;bit++;
  if (Str.charAt(19)!='-') xx+=bit;bit++;
  if (Str.charAt(18)!='-') xx+=bit;bit++;
  if (Str.charAt(17)!='-') xx+=bit;bit++;
  if (Str.charAt(16)!='-') xx+=bit;bit++;
  if (Str.charAt(15)!='-') xx+=bit;bit++;
  if (Str.charAt(14)!='-') xx+=bit;bit++;
  if (Str.charAt(13)!='-') xx+=bit;bit++;
  if (Str.charAt(12)!='-') xx+=bit;bit++;
  if (Str.charAt(11)!='-') xx+=bit;bit++;
  if (Str.charAt(10)!='-') xx+=bit;bit++;
  if (Str.charAt(9)!='-') xx+=bit;bit++;
  if (Str.charAt(8)!='-') xx+=bit;bit++;
  if (Str.charAt(7)!='-') xx+=bit;bit++;
  if (Str.charAt(6)!='-') xx+=bit;bit++;
  if (Str.charAt(5)!='-') xx+=bit;bit++;
  if (Str.charAt(4)!='-') xx+=bit;bit++;
  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

public class test {
     public static void main(String[] args) {
         String str = "lkee-sli2<F2-8s0--slk3vUIGN_-";
         str = str.replaceAll("-","");
         int len = str.length();
         long xx = len * (len + 1) / 2;
         System.out.println(xx);
     }

}

That was just a sample. You have used Arithmetic Addition.

Say the function is little complex.

Below is the sample Java Code:

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

long xx=0; long bit=1;

if (Str.charAt(50)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(49)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(48)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(47)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(46)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(45)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(44)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(43)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(42)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(41)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(40)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(39)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(38)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(37)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(36)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(35)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(34)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(33)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(32)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(31)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(30)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(29)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(28)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(27)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(26)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(25)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(24)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(23)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(22)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(21)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(20)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(19)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(18)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(17)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(16)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(15)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(14)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(13)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(12)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(11)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(10)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(9)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(8)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(7)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(6)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(5)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(4)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(3)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(2)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(1)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(0)!='-') xx+=bit;bit=bit<<1;


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

What will you do in this case?

Bye
Sanny
 
K

Knute Johnson

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++;
if (Str.charAt(47)!='-') xx+=bit;bit++;
if (Str.charAt(46)!='-') xx+=bit;bit++;
if (Str.charAt(45)!='-') xx+=bit;bit++;
if (Str.charAt(44)!='-') xx+=bit;bit++;
if (Str.charAt(43)!='-') xx+=bit;bit++;
if (Str.charAt(42)!='-') xx+=bit;bit++;
if (Str.charAt(41)!='-') xx+=bit;bit++;
if (Str.charAt(40)!='-') xx+=bit;bit++;
if (Str.charAt(39)!='-') xx+=bit;bit++;
if (Str.charAt(38)!='-') xx+=bit;bit++;
if (Str.charAt(37)!='-') xx+=bit;bit++;
if (Str.charAt(36)!='-') xx+=bit;bit++;
if (Str.charAt(35)!='-') xx+=bit;bit++;
if (Str.charAt(34)!='-') xx+=bit;bit++;
if (Str.charAt(33)!='-') xx+=bit;bit++;
if (Str.charAt(32)!='-') xx+=bit;bit++;
if (Str.charAt(31)!='-') xx+=bit;bit++;
if (Str.charAt(30)!='-') xx+=bit;bit++;
if (Str.charAt(29)!='-') xx+=bit;bit++;
if (Str.charAt(28)!='-') xx+=bit;bit++;
if (Str.charAt(27)!='-') xx+=bit;bit++;
if (Str.charAt(26)!='-') xx+=bit;bit++;
if (Str.charAt(25)!='-') xx+=bit;bit++;
if (Str.charAt(24)!='-') xx+=bit;bit++;
if (Str.charAt(23)!='-') xx+=bit;bit++;
if (Str.charAt(22)!='-') xx+=bit;bit++;
if (Str.charAt(21)!='-') xx+=bit;bit++;
if (Str.charAt(20)!='-') xx+=bit;bit++;
if (Str.charAt(19)!='-') xx+=bit;bit++;
if (Str.charAt(18)!='-') xx+=bit;bit++;
if (Str.charAt(17)!='-') xx+=bit;bit++;
if (Str.charAt(16)!='-') xx+=bit;bit++;
if (Str.charAt(15)!='-') xx+=bit;bit++;
if (Str.charAt(14)!='-') xx+=bit;bit++;
if (Str.charAt(13)!='-') xx+=bit;bit++;
if (Str.charAt(12)!='-') xx+=bit;bit++;
if (Str.charAt(11)!='-') xx+=bit;bit++;
if (Str.charAt(10)!='-') xx+=bit;bit++;
if (Str.charAt(9)!='-') xx+=bit;bit++;
if (Str.charAt(8)!='-') xx+=bit;bit++;
if (Str.charAt(7)!='-') xx+=bit;bit++;
if (Str.charAt(6)!='-') xx+=bit;bit++;
if (Str.charAt(5)!='-') xx+=bit;bit++;
if (Str.charAt(4)!='-') xx+=bit;bit++;
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

public class test {
public static void main(String[] args) {
String str = "lkee-sli2<F2-8s0--slk3vUIGN_-";
str = str.replaceAll("-","");
int len = str.length();
long xx = len * (len + 1) / 2;
System.out.println(xx);
}

}

That was just a sample. You have used Arithmetic Addition.

Say the function is little complex.

Below is the sample Java Code:

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

long xx=0; long bit=1;

if (Str.charAt(50)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(49)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(48)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(47)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(46)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(45)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(44)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(43)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(42)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(41)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(40)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(39)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(38)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(37)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(36)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(35)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(34)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(33)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(32)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(31)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(30)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(29)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(28)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(27)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(26)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(25)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(24)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(23)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(22)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(21)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(20)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(19)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(18)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(17)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(16)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(15)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(14)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(13)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(12)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(11)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(10)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(9)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(8)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(7)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(6)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(5)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(4)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(3)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(2)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(1)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(0)!='-') xx+=bit;bit=bit<<1;


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

What will you do in this case?

Bye
Sanny

You should post what you want to know about. But this case is even
simpler, just shift bit the number of non - characters.

bit << len;
 
S

Sanny

What about this case?

if (Str.charAt(50)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(49)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(48)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(47)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(46)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(45)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(44)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(43)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(42)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(41)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(40)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(39)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(38)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(37)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(36)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(35)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(34)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(33)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(32)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(31)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(30)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(29)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(28)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(27)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(26)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(25)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(24)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(23)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(22)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(21)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(20)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(19)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(18)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(17)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(16)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(15)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(14)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(13)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(12)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(11)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(10)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(9)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(8)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(7)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(6)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(5)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(4)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(3)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(2)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(1)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(0)!='-') xx+=bit;bit=bit*2;

Bye
Sanny
 
K

Knute Johnson

On 7/14/2010 11:16 AM, Sanny wrote:





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++;
if (Str.charAt(47)!='-') xx+=bit;bit++;
if (Str.charAt(46)!='-') xx+=bit;bit++;
if (Str.charAt(45)!='-') xx+=bit;bit++;
if (Str.charAt(44)!='-') xx+=bit;bit++;
if (Str.charAt(43)!='-') xx+=bit;bit++;
if (Str.charAt(42)!='-') xx+=bit;bit++;
if (Str.charAt(41)!='-') xx+=bit;bit++;
if (Str.charAt(40)!='-') xx+=bit;bit++;
if (Str.charAt(39)!='-') xx+=bit;bit++;
if (Str.charAt(38)!='-') xx+=bit;bit++;
if (Str.charAt(37)!='-') xx+=bit;bit++;
if (Str.charAt(36)!='-') xx+=bit;bit++;
if (Str.charAt(35)!='-') xx+=bit;bit++;
if (Str.charAt(34)!='-') xx+=bit;bit++;
if (Str.charAt(33)!='-') xx+=bit;bit++;
if (Str.charAt(32)!='-') xx+=bit;bit++;
if (Str.charAt(31)!='-') xx+=bit;bit++;
if (Str.charAt(30)!='-') xx+=bit;bit++;
if (Str.charAt(29)!='-') xx+=bit;bit++;
if (Str.charAt(28)!='-') xx+=bit;bit++;
if (Str.charAt(27)!='-') xx+=bit;bit++;
if (Str.charAt(26)!='-') xx+=bit;bit++;
if (Str.charAt(25)!='-') xx+=bit;bit++;
if (Str.charAt(24)!='-') xx+=bit;bit++;
if (Str.charAt(23)!='-') xx+=bit;bit++;
if (Str.charAt(22)!='-') xx+=bit;bit++;
if (Str.charAt(21)!='-') xx+=bit;bit++;
if (Str.charAt(20)!='-') xx+=bit;bit++;
if (Str.charAt(19)!='-') xx+=bit;bit++;
if (Str.charAt(18)!='-') xx+=bit;bit++;
if (Str.charAt(17)!='-') xx+=bit;bit++;
if (Str.charAt(16)!='-') xx+=bit;bit++;
if (Str.charAt(15)!='-') xx+=bit;bit++;
if (Str.charAt(14)!='-') xx+=bit;bit++;
if (Str.charAt(13)!='-') xx+=bit;bit++;
if (Str.charAt(12)!='-') xx+=bit;bit++;
if (Str.charAt(11)!='-') xx+=bit;bit++;
if (Str.charAt(10)!='-') xx+=bit;bit++;
if (Str.charAt(9)!='-') xx+=bit;bit++;
if (Str.charAt(8)!='-') xx+=bit;bit++;
if (Str.charAt(7)!='-') xx+=bit;bit++;
if (Str.charAt(6)!='-') xx+=bit;bit++;
if (Str.charAt(5)!='-') xx+=bit;bit++;
if (Str.charAt(4)!='-') xx+=bit;bit++;
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

public class test {
public static void main(String[] args) {
String str = "lkee-sli2<F2-8s0--slk3vUIGN_-";
str = str.replaceAll("-","");
int len = str.length();
long xx = len * (len + 1) / 2;
System.out.println(xx);
}

}

That was just a sample. You have used Arithmetic Addition.

Say the function is little complex.

Below is the sample Java Code:

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

long xx=0; long bit=1;

if (Str.charAt(50)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(49)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(48)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(47)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(46)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(45)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(44)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(43)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(42)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(41)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(40)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(39)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(38)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(37)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(36)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(35)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(34)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(33)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(32)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(31)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(30)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(29)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(28)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(27)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(26)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(25)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(24)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(23)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(22)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(21)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(20)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(19)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(18)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(17)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(16)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(15)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(14)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(13)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(12)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(11)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(10)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(9)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(8)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(7)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(6)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(5)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(4)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(3)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(2)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(1)!='-') xx+=bit;bit=bit<<1;
if (Str.charAt(0)!='-') xx+=bit;bit=bit<<1;


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

What will you do in this case?

Bye
Sanny

You should post what you want to know about. But this case is even
simpler, just shift bit the number of non - characters.

bit << len;

I take that back. It is more complex but you still don't need all those
if statements.
 
S

Sanny

On 7/14/2010 11:16 AM, Sanny wrote:
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++;
   if (Str.charAt(47)!='-') xx+=bit;bit++;
   if (Str.charAt(46)!='-') xx+=bit;bit++;
   if (Str.charAt(45)!='-') xx+=bit;bit++;
   if (Str.charAt(44)!='-') xx+=bit;bit++;
   if (Str.charAt(43)!='-') xx+=bit;bit++;
   if (Str.charAt(42)!='-') xx+=bit;bit++;
   if (Str.charAt(41)!='-') xx+=bit;bit++;
   if (Str.charAt(40)!='-') xx+=bit;bit++;
   if (Str.charAt(39)!='-') xx+=bit;bit++;
   if (Str.charAt(38)!='-') xx+=bit;bit++;
   if (Str.charAt(37)!='-') xx+=bit;bit++;
   if (Str.charAt(36)!='-') xx+=bit;bit++;
   if (Str.charAt(35)!='-') xx+=bit;bit++;
   if (Str.charAt(34)!='-') xx+=bit;bit++;
   if (Str.charAt(33)!='-') xx+=bit;bit++;
   if (Str.charAt(32)!='-') xx+=bit;bit++;
   if (Str.charAt(31)!='-') xx+=bit;bit++;
   if (Str.charAt(30)!='-') xx+=bit;bit++;
   if (Str.charAt(29)!='-') xx+=bit;bit++;
   if (Str.charAt(28)!='-') xx+=bit;bit++;
   if (Str.charAt(27)!='-') xx+=bit;bit++;
   if (Str.charAt(26)!='-') xx+=bit;bit++;
   if (Str.charAt(25)!='-') xx+=bit;bit++;
   if (Str.charAt(24)!='-') xx+=bit;bit++;
   if (Str.charAt(23)!='-') xx+=bit;bit++;
   if (Str.charAt(22)!='-') xx+=bit;bit++;
   if (Str.charAt(21)!='-') xx+=bit;bit++;
   if (Str.charAt(20)!='-') xx+=bit;bit++;
   if (Str.charAt(19)!='-') xx+=bit;bit++;
   if (Str.charAt(18)!='-') xx+=bit;bit++;
   if (Str.charAt(17)!='-') xx+=bit;bit++;
   if (Str.charAt(16)!='-') xx+=bit;bit++;
   if (Str.charAt(15)!='-') xx+=bit;bit++;
   if (Str.charAt(14)!='-') xx+=bit;bit++;
   if (Str.charAt(13)!='-') xx+=bit;bit++;
   if (Str.charAt(12)!='-') xx+=bit;bit++;
   if (Str.charAt(11)!='-') xx+=bit;bit++;
   if (Str.charAt(10)!='-') xx+=bit;bit++;
   if (Str.charAt(9)!='-') xx+=bit;bit++;
   if (Str.charAt(8)!='-') xx+=bit;bit++;
   if (Str.charAt(7)!='-') xx+=bit;bit++;
   if (Str.charAt(6)!='-') xx+=bit;bit++;
   if (Str.charAt(5)!='-') xx+=bit;bit++;
   if (Str.charAt(4)!='-') xx+=bit;bit++;
   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
public class test {
      public static void main(String[] args) {
          String str = "lkee-sli2<F2-8s0--slk3vUIGN_-";
          str = str.replaceAll("-","");
          int len = str.length();
          long xx = len * (len + 1) / 2;
          System.out.println(xx);
      }
}
That was just a sample. You have used Arithmetic Addition.
Say the function is little complex.
Below is the sample Java Code:

long xx=0; long bit=1;
  if (Str.charAt(50)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(49)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(48)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(47)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(46)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(45)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(44)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(43)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(42)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(41)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(40)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(39)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(38)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(37)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(36)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(35)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(34)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(33)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(32)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(31)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(30)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(29)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(28)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(27)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(26)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(25)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(24)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(23)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(22)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(21)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(20)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(19)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(18)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(17)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(16)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(15)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(14)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(13)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(12)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(11)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(10)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(9)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(8)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(7)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(6)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(5)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(4)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(3)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(2)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(1)!='-') xx+=bit;bit=bit<<1;
  if (Str.charAt(0)!='-') xx+=bit;bit=bit<<1;

What will you do in this case?
Bye
Sanny

You should post what you want to know about.  But this case is even
simpler, just shift bit the number of non - characters.

bit << len;

The if Condition works only on "xx+=bit;" and "bit=bit<<1;" is outside
the if condition.

You may Change "bit=bit<<1;" into bit=bit*2;

Then the conditions will be as follows.

if (Str.charAt(50)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(49)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(48)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(47)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(46)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(45)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(44)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(43)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(42)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(41)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(40)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(39)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(38)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(37)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(36)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(35)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(34)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(33)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(32)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(31)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(30)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(29)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(28)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(27)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(26)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(25)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(24)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(23)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(22)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(21)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(20)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(19)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(18)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(17)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(16)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(15)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(14)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(13)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(12)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(11)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(10)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(9)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(8)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(7)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(6)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(5)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(4)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(3)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(2)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(1)!='-') xx+=bit;bit=bit*2;
if (Str.charAt(0)!='-') xx+=bit;bit=bit*2;


Bye
Sanny
 
S

Sanny

I take that back.  It is more complex but you still don't need all those
if statements.

I agree. I want a simple equation instead of looking for 50 if
conditions.

Simple addition/ multiplication/ division will be faster than 50 if
conditions.

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
 
S

Sanny

Or even (to micro-optimize a bit :) ):
   long xx = 0, bit = 1 << 50;

   for (int i = 0; i <= 50; i++)
   {
     if (Str.charAt(i) != '-')
     {
       xx |= bit;
     }

     bit = bit >> 1;
   }

You are correct. I removed the above for loop to increase performance
as

for (int i = 0; i <= 50; i++) here "i<=50" if condition is executed So
there are 2 if condition per loop.

By removing the for loop I avoided one if condition.

Now, I am looking for some way to use single equation/ Algebric may be
to remove all if conditions.

I find if conditions take 5-10 times longer than simple arithmetic
works.

Bye
Sanny
 
S

Screamin Lord Byron

Now, I am looking for some way to use single equation/ Algebric may be
to remove all if conditions.

Like Peter said, first describe exactly what you want (in English, not
Java). If you don't do that, no one will be able to help you, because I
doubt anyone here can read minds.

And there is no need to repeat yourself. Just describe the problem in
it's entirety.
 
D

Daniel Pitts

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;
I'm going to take the liberty of replacing this with a for loop:
for (int i = str.length()-1; i>=0; --i) {
if (str.charAt(i) != '-') {
xx += bit;
}
bit += bit; // equiv of bit <<= 1;
}
================================

Since most of the time taken is by the "if condition."
What evidence do you have to support that?
What is "a long time" to you? I would imagine this method executes in
under a millisecond. What tools have you used to determine the speed?
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
You have a conditional operation, so you need to handle it. My guess is
that you are barking up the wrong tree with this form of micro optimization.
 
S

Sanny

Have you considered a lookup-table? It would have 65536 elements, but
most of the accesses would probably be concentrated in a few areas, so
it would be quite cache-friendly. You could map to e.g. 1 for "-" and 0
for anything else, then do arithmetic on the lookup result.

Problem Solved. I tried a lookup table and now all if conditions have
been removed.

Now its just a piece of addition. Which quite fast 3-4 times faster
than the if conditions.

Bye
Sanny
 
D

Daniel Pitts

Problem Solved. I tried a lookup table and now all if conditions have
been removed.

Now its just a piece of addition. Which quite fast 3-4 times faster
than the if conditions.
I doubt it.
A lookup table requires a dereference and a multiplication.

I *really* think you need to make sure you're not complicating a simple
piece of code for little actual gain. Measure twice, cut once.
 
T

Tom Anderson

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++;
if (Str.charAt(47)!='-') xx+=bit;bit++;
if (Str.charAt(46)!='-') xx+=bit;bit++;
if (Str.charAt(45)!='-') xx+=bit;bit++;
if (Str.charAt(44)!='-') xx+=bit;bit++;
if (Str.charAt(43)!='-') xx+=bit;bit++;
if (Str.charAt(42)!='-') xx+=bit;bit++;
if (Str.charAt(41)!='-') xx+=bit;bit++;
if (Str.charAt(40)!='-') xx+=bit;bit++;
if (Str.charAt(39)!='-') xx+=bit;bit++;
if (Str.charAt(38)!='-') xx+=bit;bit++;
if (Str.charAt(37)!='-') xx+=bit;bit++;
if (Str.charAt(36)!='-') xx+=bit;bit++;
if (Str.charAt(35)!='-') xx+=bit;bit++;
if (Str.charAt(34)!='-') xx+=bit;bit++;
if (Str.charAt(33)!='-') xx+=bit;bit++;
if (Str.charAt(32)!='-') xx+=bit;bit++;
if (Str.charAt(31)!='-') xx+=bit;bit++;
if (Str.charAt(30)!='-') xx+=bit;bit++;
if (Str.charAt(29)!='-') xx+=bit;bit++;
if (Str.charAt(28)!='-') xx+=bit;bit++;
if (Str.charAt(27)!='-') xx+=bit;bit++;
if (Str.charAt(26)!='-') xx+=bit;bit++;
if (Str.charAt(25)!='-') xx+=bit;bit++;
if (Str.charAt(24)!='-') xx+=bit;bit++;
if (Str.charAt(23)!='-') xx+=bit;bit++;
if (Str.charAt(22)!='-') xx+=bit;bit++;
if (Str.charAt(21)!='-') xx+=bit;bit++;
if (Str.charAt(20)!='-') xx+=bit;bit++;
if (Str.charAt(19)!='-') xx+=bit;bit++;
if (Str.charAt(18)!='-') xx+=bit;bit++;
if (Str.charAt(17)!='-') xx+=bit;bit++;
if (Str.charAt(16)!='-') xx+=bit;bit++;
if (Str.charAt(15)!='-') xx+=bit;bit++;
if (Str.charAt(14)!='-') xx+=bit;bit++;
if (Str.charAt(13)!='-') xx+=bit;bit++;
if (Str.charAt(12)!='-') xx+=bit;bit++;
if (Str.charAt(11)!='-') xx+=bit;bit++;
if (Str.charAt(10)!='-') xx+=bit;bit++;
if (Str.charAt(9)!='-') xx+=bit;bit++;
if (Str.charAt(8)!='-') xx+=bit;bit++;
if (Str.charAt(7)!='-') xx+=bit;bit++;
if (Str.charAt(6)!='-') xx+=bit;bit++;
if (Str.charAt(5)!='-') xx+=bit;bit++;
if (Str.charAt(4)!='-') xx+=bit;bit++;
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."

So, the fundamental problem is that you are insane.

But you could do something like this:

long xx = 0;
for (int i = 0; i < 50; ++i) {
int d = Str.charAt(i) ^ '-';
d = d | (d >> 8);
d = d | (d >> 4);
d = d | (d >> 2);
d = d | (d >> 1);
xx = (xx << 1) | (d & 1);
}

I don't think there's any other way to do this without a formal
conditional. However, on some architectures, those can be compiled to
predicate instructions. I assume the root of this is that you're worried
about pipeline bubbles. Also, the insanity, of course.
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"

The Math Equation you need is built on an understanding that an integer is
a vector of bits, and that you can use bitwise operators to carry out
parallel mathematical operations on those bits.

There are faster ways to do the bit-counting part than my sequence of
shifts and ors, but those are in the realm of advanced binary voodoo, and
i am not at liberty to reveal them to you.
Please suggest some equation or any other way to do this in one
statement.

One statement? More difficult.
ENUMERATION? can these be used here to solve these "if conditions" in
one step?

No.

You could use a lookup table, though.

tom
 
L

Lew

Sanny, you should attribute quotes.

Problem Solved. I tried a lookup table and now all if conditions have
been removed.

Now its just a piece of addition. Which quite fast 3-4 times faster
than the if conditions.

I'm curious, Sanny, why you have not answered the several people who asked how
you obtained these speed conclusions. I, for one, was on tenterhooks for the
answers, and only forbore from echoing the question before now because I felt
so sure that after four or five posts of the same question you would actually
deign to answer it. Alas to be so disappointed.

And we're reasonably clued in enough to guess that you ran a benchmark. The
answer needs to be rather white-box: how do you distinguish the time in "if"
evaluation from that in other areas of code? What were the hard numbers for
the loop and unrolled version? What Hotspot warmup protocol did you use? Did
you exclude JVM startup time? 3-4 times faster than what time, exactly?

Microbenchmarks are notoriously unreliable estimators of real-world
performance, especially for Java. Things like running benchmarks under
different load conditions or different threading scenarios over different
numbers of CPUs from a production version are infamous legends in the world of
performance engineering. I for one am deeply dubious of your benchmark
claims, vague as they are and unaccompanied by a detailed description of your
testing rigors, much less actual code.

Please provide the requisite fill for the gaps in our knowledge.
 
S

Sanny

Microbenchmarks are notoriously unreliable estimators of real-world
performance, especially for Java.  Things like running benchmarks under
different load conditions or different threading scenarios over different
numbers of CPUs from a production version are infamous legends in the world of
performance engineering.  I for one am deeply dubious of your benchmark
claims, vague as they are and unaccompanied by a detailed description of your
testing rigors, much less actual code.

Please provide the requisite fill for the gaps in our knowledge.

I tried a for loop and found "if conditions" take more time.

Just create below program.

Now make 2 for loops and see which one is faster.

======================
for (int i=0;i<100000;i++){

if (xxx==0) vvv=100; else {
if (xxx==1) vvv=200;
}// else

if (xxx==0) vvv=100; else {
if (xxx==1) vvv=200;
}// else

if (xxx==0) vvv=100; else {
if (xxx==1) vvv=200;
}// else

}// for loop
=======================

Now consider removing the if conditions.

====================
int[] mm int[2];

mm[0]=100; mm[1]=200;

for (int i=0;i<100000;i++){

vvvv=mm[xxx];

vvvv=mm[xxx];

vvvv=mm[xxx];

}// for loop
==================

Above two functions do same thing that is
1. if xxx==0 then vvv=100
1. if xxx==1 then vvv=200

But second forloop will be 10-20 times faster as it do not contains
the "if conditions."

Try and test it yourself.

Bye
Sanny

Java Programmers can Earn $500 - $5000 per month
Visit: http://www.GetClub.com/Experts.php
 
L

Lew

Sanny, attribute your quotes!

[Lew wrote:]
I tried a for loop and found "if conditions" take more time.

Just create below program.

The for loops you show (without indentation) are different from what you were
doing in the original problem statement.
======================
for (int i=0;i<100000;i++){

if (xxx==0) vvv=100; else {
if (xxx==1) vvv=200;
}// else

if (xxx==0) vvv=100; else {
if (xxx==1) vvv=200;
}// else

if (xxx==0) vvv=100; else {
if (xxx==1) vvv=200;
}// else

}// for loop
=======================

Now consider removing the if conditions.

====================
int[] mm int[2];

mm[0]=100; mm[1]=200;

for (int i=0;i<100000;i++){

vvvv=mm[xxx];

vvvv=mm[xxx];

vvvv=mm[xxx];

}// for loop
==================

Above two functions do same thing that is
1. if xxx==0 then vvv=100
1. if xxx==1 then vvv=200

But second forloop will be 10-20 times faster as it do not contains
the "if conditions."

Try and test it yourself.


How about you answer first?

You have revealed nothing of your benchmark. The snippets you show in your
response do not do what your original problem statement specified. You don't
show your measurement techniques. You don't even explain your testing rigor,
much less share the benchmark code.

What exactly should I try?
 
S

Sanny

You have revealed nothing of your benchmark. The snippets you show in your
response do not do what your original problem statement specified.  You don't
show your measurement techniques.  You don't even explain your testing rigor,
much less share the benchmark code.

What exactly should I try?

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.

I tested these an year back. and found if conditions are taking a lot
of time than other arithmetic problems.

Bye
Sanny
 
L

Lew

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.

I tested these an year back. and found if conditions are taking a lot
of time than other arithmetic problems.

You really have a gift for avoiding the question.

Why should I put in the extra "efforts" that you won't deliver? I ask
again, where are your timing loops? Where is your code? What is your
rigor?

I'm not going to waste my time performing a useless microbenchmark to
answer a question you refuse to answer, knowing that my way of doing
it will be more correct and produce different results from yours.

Of course the new example you post will run quicker without 'if'
evaluations, but that says less than nothing about the very different
scenario that you first presented, where some form of conditional
logic was needed, nor does it reveal the actual numbers from your test
runs, nor does it reveal how much the 13 ms you saved with all your
complicated micro-optimization counts against the 30 minutes your
program waits for input.

I suppose I can safely conclude that you will continue to refuse to
answer. I shall return the favor.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top