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

  • Follow


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
0
Reply softtanks22 (33) 7/14/2010 6:16:45 PM

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);
     }
}

-- 

Knute Johnson
email s/nospam/knute2010/

0
Reply Knute 7/14/2010 6:40:02 PM


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);
     }
}

-- 

Knute Johnson
email s/nospam/knute2010/

0
Reply Knute 7/14/2010 6:41:29 PM

On Jul 14, 11:40=A0pm, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
wrote:
> 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:
>
> > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D
>
> > long xx=3D0; long bit=3D1;
>
> > =A0 if (Str.charAt(50)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(49)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(48)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(47)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(46)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(45)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(44)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(43)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(42)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(41)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(40)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(39)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(38)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(37)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(36)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(35)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(34)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(33)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(32)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(31)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(30)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(29)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(28)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(27)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(26)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(25)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(24)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(23)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(22)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(21)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(20)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(19)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(18)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(17)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(16)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(15)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(14)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(13)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(12)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(11)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(10)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(9)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(8)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(7)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(6)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(5)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(4)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(3)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(2)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(1)!=3D'-') xx+=3Dbit;bit++;
> > =A0 if (Str.charAt(0)!=3D'-') xx+=3Dbit;bit++;
>
> > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D
>
> > Since most of the time taken is by the "if condition."
>
> > I am thinking of using
>
> > char mm=3D'-';
> > =A0 =A0int mm1=3DCharacter.getNumericValue(mm);
>
> > int mm2=3DStr.charAt(0);
>
> > int mm3=3Dmm2-mm1;
>
> > Now if I subtract mm2-mm1=3Dmm3 Then I have to have xx+=3Dbit; only whe=
n
> > mm3=3D=3D0;
>
> > I want to write an Math Equation that computes mm3 and when mm3 value
> > is "0" xx+=3Dbit; 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 {
> =A0 =A0 =A0public static void main(String[] args) {
> =A0 =A0 =A0 =A0 =A0String str =3D "lkee-sli2<F2-8s0--slk3vUIGN_-";
> =A0 =A0 =A0 =A0 =A0str =3D str.replaceAll("-","");
> =A0 =A0 =A0 =A0 =A0int len =3D str.length();
> =A0 =A0 =A0 =A0 =A0long xx =3D len * (len + 1) / 2;
> =A0 =A0 =A0 =A0 =A0System.out.println(xx);
> =A0 =A0 =A0}
>
> }

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

Say the function is little complex.

Below is the sample Java Code:

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D

long xx=3D0; long bit=3D1;

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


=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D

What will you do in this case?

Bye
Sanny





0
Reply softtanks22 (33) 7/14/2010 6:49:02 PM

On 7/14/2010 11:49 AM, Sanny wrote:
> On Jul 14, 11:40 pm, Knute Johnson<nos...@rabbitbrush.frazmtn.com>
> wrote:
>> 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;

-- 

Knute Johnson
email s/nospam/knute2010/

0
Reply nospam8071 (917) 7/14/2010 6:50:52 PM

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


0
Reply Sanny 7/14/2010 6:51:27 PM

On 7/14/2010 11:50 AM, Knute Johnson wrote:
> On 7/14/2010 11:49 AM, Sanny wrote:
>> On Jul 14, 11:40 pm, Knute Johnson<nos...@rabbitbrush.frazmtn.com>
>> wrote:
>>> 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.

-- 

Knute Johnson
email s/nospam/knute2010/

0
Reply nospam8071 (917) 7/14/2010 6:53:18 PM

On Jul 14, 11:50=A0pm, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
wrote:
> On 7/14/2010 11:49 AM, Sanny wrote:
>
>
>
>
>
> > On Jul 14, 11:40 pm, Knute Johnson<nos...@rabbitbrush.frazmtn.com>
> > wrote:
> >> 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:
>
> >>> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D
>
> >>> long xx=3D0; long bit=3D1;
>
> >>> =A0 =A0if (Str.charAt(50)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(49)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(48)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(47)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(46)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(45)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(44)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(43)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(42)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(41)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(40)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(39)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(38)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(37)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(36)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(35)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(34)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(33)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(32)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(31)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(30)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(29)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(28)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(27)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(26)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(25)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(24)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(23)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(22)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(21)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(20)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(19)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(18)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(17)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(16)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(15)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(14)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(13)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(12)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(11)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(10)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(9)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(8)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(7)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(6)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(5)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(4)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(3)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(2)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(1)!=3D'-') xx+=3Dbit;bit++;
> >>> =A0 =A0if (Str.charAt(0)!=3D'-') xx+=3Dbit;bit++;
>
> >>> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D
>
> >>> Since most of the time taken is by the "if condition."
>
> >>> I am thinking of using
>
> >>> char mm=3D'-';
> >>> =A0 =A0 int mm1=3DCharacter.getNumericValue(mm);
>
> >>> int mm2=3DStr.charAt(0);
>
> >>> int mm3=3Dmm2-mm1;
>
> >>> Now if I subtract mm2-mm1=3Dmm3 Then I have to have xx+=3Dbit; only w=
hen
> >>> mm3=3D=3D0;
>
> >>> I want to write an Math Equation that computes mm3 and when mm3 value
> >>> is "0" xx+=3Dbit; 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 {
> >> =A0 =A0 =A0 public static void main(String[] args) {
> >> =A0 =A0 =A0 =A0 =A0 String str =3D "lkee-sli2<F2-8s0--slk3vUIGN_-";
> >> =A0 =A0 =A0 =A0 =A0 str =3D str.replaceAll("-","");
> >> =A0 =A0 =A0 =A0 =A0 int len =3D str.length();
> >> =A0 =A0 =A0 =A0 =A0 long xx =3D len * (len + 1) / 2;
> >> =A0 =A0 =A0 =A0 =A0 System.out.println(xx);
> >> =A0 =A0 =A0 }
>
> >> }
>
> > That was just a sample. You have used Arithmetic Addition.
>
> > Say the function is little complex.
>
> > Below is the sample Java Code:
>
> > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D
>
> > long xx=3D0; long bit=3D1;
>
> > =A0 if (Str.charAt(50)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(49)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(48)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(47)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(46)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(45)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(44)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(43)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(42)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(41)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(40)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(39)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(38)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(37)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(36)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(35)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(34)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(33)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(32)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(31)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(30)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(29)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(28)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(27)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(26)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(25)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(24)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(23)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(22)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(21)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(20)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(19)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(18)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(17)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(16)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(15)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(14)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(13)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(12)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(11)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(10)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(9)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(8)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(7)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(6)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(5)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(4)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(3)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(2)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(1)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
> > =A0 if (Str.charAt(0)!=3D'-') xx+=3Dbit;bit=3Dbit<<1;
>
> > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D
>
> > What will you do in this case?
>
> > Bye
> > Sanny
>
> You should post what you want to know about. =A0But this case is even
> simpler, just shift bit the number of non - characters.
>
> bit << len;

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

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

Then the conditions will be as follows.

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


Bye
Sanny



0
Reply softtanks22 (33) 7/14/2010 6:54:25 PM

> I take that back. =A0It is more complex but you still don't need all thos=
e
> 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=3D'-';
  int mm1=3DCharacter.getNumericValue(mm);

int mm2=3DStr.charAt(0);

int mm3=3Dmm2-mm1;

Now if I subtract mm2-mm1=3Dmm3 Then I have to have xx+=3Dbit; only when
mm3=3D=3D0;

I want to write an Math Equation that computes mm3 and when mm3 value
is "0" xx+=3Dbit; 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
0
Reply Sanny 7/14/2010 6:58:52 PM

Sanny wrote:
> On Jul 14, 11:50 pm, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
> wrote:
>> [...]
>> 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;  [...]

With all of the various examples you've provided, it's very difficult to 
understand what the heck you are really trying to do.  As Knute already 
said, you should explain _exactly_ what the goal here is.

But, even a na�ve implementation of your original code or any variation 
thereof is far better than what you've posted.  It's hard to even 
understand how someone who has any real experience programming has led 
themselves to essentially unroll a loop to the extent shown in your 
examples.

What would be wrong with simply writing the code like this:

   long xx = 0, bit = 1;

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

     bit = bit << 1;
   }

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;
   }

?

Pete
0
Reply Peter 7/14/2010 7:06:40 PM

> Or even (to micro-optimize a bit :) ):
>
> =A0 =A0long xx =3D 0, bit =3D 1 << 50;
>
> =A0 =A0for (int i =3D 0; i <=3D 50; i++)
> =A0 =A0{
> =A0 =A0 =A0if (Str.charAt(i) !=3D '-')
> =A0 =A0 =A0{
> =A0 =A0 =A0 =A0xx |=3D bit;
> =A0 =A0 =A0}
>
> =A0 =A0 =A0bit =3D bit >> 1;
> =A0 =A0}

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

for (int i =3D 0; i <=3D 50; i++) here "i<=3D50" if condition is executed S=
o
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



0
Reply softtanks22 (33) 7/14/2010 7:11:43 PM

Sanny wrote:
>> 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.

Given the problem statement so far, you cannot avoid the comparisons. 
The only thing your code appears to deal with is converting a string 
with hyphens in it into a bitfield where each bit is set in the location 
corresponding to the character position with hyphens.  At some point, 
you have to inspect the string and convert hyphens to bits.

Your code, on most architectures, will cause problems for two reasons:

   � it has hard-coded the specific character position for each 
comparison, leading to the excessive length of the code, making it 
harder for the code to remain in the cache and for branch-prediction 
tables to stay filled.

   � it inspects the string in reverse order, which may be more likely 
to negate any benefit of data caching, as data is more likely to be 
fetched from memory address locations at and beyond that which the code 
is actually using.

The versions of the code I posted address those issues, by making the 
code much more compact and (in the case of the second example), 
inspecting the string in the forward direction.

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

Java doesn't have any way to perform mathematical operations on strings. 
  At best, you could perform mathematical operations on individual 
character values, but at that point you might as well just do a comparison.

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

You base that claim on what evidence?

> 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;

How is a subtraction and a comparison against 0 any better than a 
comparison against the character value for '-'?  That is:

   if (Str.charAt(0) - mm1 == 0)
   {
     xx += bit;
   }

Or alternatively, how is a subtraction, multiplication and unconditional 
addition/bitwise-or better?  That is:

   xx += bit * (Str.charAt(0) - mm1);

I fail to see why that is necessarily going to be more efficient than 
just doing a straight comparison.

Pete
0
Reply Peter 7/14/2010 7:22:19 PM

Sanny wrote:
>> 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.

Loop-unrolling _may_ improve performance.  But it doesn't always, and 
when done to an excessive degree can actually _hurt_ performance (for 
the reasons I mentioned elsewhere).

> 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.

Again, you find that how?

Pete
0
Reply Peter 7/14/2010 7:24:19 PM

On 07/14/2010 09:11 PM, Sanny wrote:

> 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.
0
Reply Screamin 7/14/2010 7:59:13 PM

Sanny wrote:
>> 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.

The time for an if condition is highly variable. It can be practically
nothing or very expensive, depending on whether the processor guesses
correctly in deciding between fetching the instructions following the
branch or the branch target. Branch predictors are designed and tested
to deal well with sort of pattern that shows up with a simple for-loop.

Meanwhile, your hand unrolling may be making it harder for the JVM to
optimize the call to charAt. It sees 50 calls each with 1/50 of the real
call frequency.

I would stick with the more normal form of the code until you are
completely happy with the algorithm, and keep it at least as comments
even if you do find you need to do micro-optimizations such as hand
unrolling.

Patricia
0
Reply Patricia 7/14/2010 8:30:45 PM

Sanny wrote:
....
> 
> I am thinking of using
> 
> char mm='-';
>   int mm1=Character.getNumericValue(mm);
> 
> int mm2=Str.charAt(0);
> 
> int mm3=mm2-mm1;

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.

Patricia
0
Reply Patricia 7/14/2010 8:36:53 PM

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;
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.


-- 
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
0
Reply Daniel 7/14/2010 9:04:56 PM

> 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


0
Reply Sanny 7/14/2010 9:11:46 PM

On 7/14/2010 2:11 PM, Sanny wrote:
>
>> 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.
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.
-- 
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
0
Reply Daniel 7/14/2010 10:30:02 PM

On Wed, 14 Jul 2010, 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."

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

-- 
I hate to advocate drugs, alcohol, violence, or insanity to anyone,
but they've always worked for me. -- Hunter S. Thompson
0
Reply Tom 7/14/2010 10:46:07 PM

Sanny, you should attribute quotes.

Patricia Shanahan wrote:
>> 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.

Sanny wrote:
> 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.

-- 
Lew
0
Reply Lew 7/14/2010 11:44:15 PM

Daniel Pitts wrote:
> On 7/14/2010 2:11 PM, Sanny wrote:
>>
>>> 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.
> 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.

Obviously, I thought the lookup table had a good enough chance of
improving performance to be worth measuring, so I am not particularly
surprised.

Modern processors have such good pipelining, superscalar, and out of
order execution capabilities that arithmetic operations are surprisingly
cheap. There are two things that really take time, cache misses and
branch mis-predictions.

The branches on the charAt results were likely to be particularly
expensive because there is no detectable pattern to them, unless the
data contains a repeating pattern. The best the branch predictor can do
is probably to predict not '-', and take a pipeline flush for each '-'.
I am surprised there was much gain from the loop unrolling - the
conditional branch in a for-loop is something branch predictors are
designed to handle well. The many calls to charAt might interfere with
in-lining.

On the other hand, the array access is likely to hit in cache. Unicode
characters come in blocks that tend to be used together. The first 128
elements of the array are the only ones used for ASCII data.

I don't believe in microbenchmarking for this sort of thing, as distinct
from running the real workload for each implementation, because cache
and branch predictor behavior is affected by the rest of the job.

Patricia
0
Reply Patricia 7/15/2010 1:32:08 AM

> Microbenchmarks are notoriously unreliable estimators of real-world
> performance, especially for Java. =A0Things 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 wor=
ld of
> performance engineering. =A0I 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.

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
for (int i=3D0;i<100000;i++){

if (xxx[i]=3D=3D0) vvv=3D100; else {
if (xxx[i]=3D=3D1) vvv=3D200;
}// else

if (xxx[i]=3D=3D0) vvv=3D100; else {
if (xxx[i]=3D=3D1) vvv=3D200;
}// else

if (xxx[i]=3D=3D0) vvv=3D100; else {
if (xxx[i]=3D=3D1) vvv=3D200;
}// else

}// for loop
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

Now consider removing the if conditions.

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
int[] mm int[2];

mm[0]=3D100; mm[1]=3D200;

for (int i=3D0;i<100000;i++){

vvvv=3Dmm[xxx[i]];

vvvv=3Dmm[xxx[i]];

vvvv=3Dmm[xxx[i]];

}// for loop
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

Above two functions do same thing that is
1. if xxx[i]=3D=3D0 then vvv=3D100
1. if xxx[i]=3D=3D1 then vvv=3D200

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

0
Reply softtanks22 (33) 7/15/2010 6:27:04 AM

Sanny, attribute your quotes!

[Lew wrote:]
>> 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.

Sanny 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[i]==0) vvv=100; else {
> if (xxx[i]==1) vvv=200;
> }// else
>
> if (xxx[i]==0) vvv=100; else {
> if (xxx[i]==1) vvv=200;
> }// else
>
> if (xxx[i]==0) vvv=100; else {
> if (xxx[i]==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[i]];
>
> vvvv=mm[xxx[i]];
>
> vvvv=mm[xxx[i]];
>
> }// for loop
> ==================
>
> Above two functions do same thing that is
> 1. if xxx[i]==0 then vvv=100
> 1. if xxx[i]==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?

-- 
Lew
0
Reply noone7 (3512) 7/15/2010 12:12:54 PM

> You have revealed nothing of your benchmark. The snippets you show in you=
r
> response do not do what your original problem statement specified. =A0You=
 don't
> show your measurement techniques. =A0You 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


0
Reply Sanny 7/15/2010 12:44:53 PM

On Jul 15, 8:44=A0am, Sanny <softtank...@hotmail.com> wrote:
> > You have revealed nothing of your benchmark. The snippets you show in y=
our
> > response do not do what your original problem statement specified. =A0Y=
ou don't
> > show your measurement techniques. =A0You don't even explain your testin=
g 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.
>

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.

--
Lew

0
Reply Lew 7/15/2010 1:07:42 PM

On 7/14/2010 11:27 PM, Sanny wrote:
>> 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[i]==0) vvv=100; else {
> if (xxx[i]==1) vvv=200;
> }// else
>
> if (xxx[i]==0) vvv=100; else {
> if (xxx[i]==1) vvv=200;
> }// else
>
> if (xxx[i]==0) vvv=100; else {
> if (xxx[i]==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[i]];
>
> vvvv=mm[xxx[i]];
>
> vvvv=mm[xxx[i]];
>
> }// for loop
> ==================
>
> Above two functions do same thing that is
> 1. if xxx[i]==0 then vvv=100
> 1. if xxx[i]==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
>

This type of micro-benchmarking is *very* unreliable on modern
processors. A few decades ago, each operation took a fixed amount of
time regardless of circumstances, and that time could be measured by
comparing the times for loops with and without the operation.

For the last couple of decades, it has been easier to increase the
number of gates on a processor chip than to reduce clock speed. The
processors we are using now are the outcome of many years of search for
ways of trading chip gates for performance. Often, that means storing
information from the code as it is executed, and using it to make later
operations run faster. The simplest case is caching of memory data - the
time to de-reference a pointer depends on how recently that cache line
has been accessed.

Because of deep, wide pipelines the processor loses a lot of time if it
has to flush the pipeline because it didn't fetch the right thing. The
solution, in the usual style of trading gates for speed, is a branch
predictor, a piece of hardware that stores information about branch
history, and uses it to make an informed guess about whether a
conditional branch will be taken or not. The processor looks at branches
entering the pipeline, and fetches either the next instruction or the
branch target depending on what the branch predictor says.

The best conclusion that can be reached from the experiment you describe
is that there exists at least one loop, with one pattern of taken and
not taken branches, in which your processor is 10 to 20 times faster
without the branch. It does not tell you anything about any other loop 
or branch pattern.

When you are taking branches depending on the contents of an array, the
cost of the branch depends on whether the data contains a pattern the
branch predictor can detect. If it does, the branches will be much
faster than if there is no pattern in the data. It will also depend on
whether one case dominates. If 90% of the branches are taken, and 10%
not taken, most branch predictors will settle down to treating the
branch as taken. They tend to do better on common idioms, the sort of
code that appeared in the benchmarks the processor designers used in
their decision making, so it is helpful to make your code as ordinary as
possible.

Patricia
0
Reply pats (3215) 7/15/2010 1:46:16 PM

On 15/07/2010 14:46, Patricia Shanahan wrote:
> 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.

-- 
RGB - slower than a bullet, less powerful than an express train ...
0
Reply RedGrittyBrick 7/15/2010 4:00:27 PM

Sanny <softtanks22@hotmail.com> writes:

> 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...


-- 
Jim Janney
0
Reply Jim 7/15/2010 5:04:21 PM

On 15-07-2010 08:44, Sanny wrote:
>> 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.

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

0
Reply ISO 7/16/2010 12:09:45 AM

Arne Vajh�j wrote:
> On 15-07-2010 08:44, Sanny wrote:
....
>> 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>.
....

I hope you are not implying that aunts are a particularly unreliable
source of information about computer performance. :-)

Patricia
0
Reply Patricia 7/16/2010 12:33:50 AM

Patricia Shanahan wrote:
> Arne Vajh�j wrote:
>> On 15-07-2010 08:44, Sanny wrote:
> ....
>>> 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>.
> ....
> 
> I hope you are not implying that aunts are a particularly unreliable
> source of information about computer performance. :-)

Maybe he is, but surely only those without their doctorates.

Or maybe it's neighbors who are unreliable.  :)
0
Reply Peter 7/16/2010 12:42:00 AM

On 15-07-2010 20:33, Patricia Shanahan wrote:
> Arne Vajh�j wrote:
>> On 15-07-2010 08:44, Sanny wrote:
> ...
>>> 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>.
> ...
>
> I hope you are not implying that aunts are a particularly unreliable
> source of information about computer performance. :-)

Just their neighbors!

:-)

Arne
0
Reply ISO 7/16/2010 1:30:41 AM

RedGrittyBrick wrote:
> On 15/07/2010 14:46, Patricia Shanahan wrote:
>> 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.
> 


This.

Make the code correct first, including correct OO design and code 
structure.  Then measure, and optimize as needed.
0
Reply markspace 7/16/2010 2:12:12 AM

On Wed, 14 Jul 2010 11:16:45 -0700 (PDT), Sanny
<softtanks22@hotmail.com> wrote, quoted or indirectly quoted someone
who said :

>
> 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.
-- 
Roedy Green Canadian Mind Products
http://mindprod.com

You encapsulate not just to save typing, but more importantly, to make it easy and safe to change the code later, since you then need change the logic in only one place. Without it, you might fail to change the logic in all the places it occurs.
0
Reply Roedy 7/16/2010 9:35:00 AM

Jim Janney <jjanney@shell.xmission.com> writes:

> Sanny <softtanks22@hotmail.com> writes:
>
>> 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.

-- 
Jim Janney
0
Reply Jim 7/16/2010 4:00:27 PM

Jim Janney wrote:
> [...]
>> 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).

Pete
0
Reply Peter 7/16/2010 5:08:19 PM

Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> writes:

> Jim Janney wrote:
>> [...]
>>> 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...

-- 
Jim Janney
0
Reply Jim 7/16/2010 5:33:43 PM

37 Replies
185 Views

(page loaded in 0.497 seconds)


Reply: