Jump to content

[JAVA] If command problem

FakezZ
Go to solution Solved by Ciccioo,

<redacted>

 

edit: actually i think i misunderstood your question

you can't write the condition like that, you have to break it into "x = 0 and y = 0"

So what do I do if I want to say 

if (x "and" y == 0){....}

Where "and" is the operator I have to put in so that the statement means "if both x and y are zero do that...". Sry just kind of a brain cramp xD

MacBook Pro 15' 2018 (Pretty much the only system I use)

Link to comment
Share on other sites

Link to post
Share on other sites

<redacted>

 

edit: actually i think i misunderstood your question

you can't write the condition like that, you have to break it into "x = 0 and y = 0"

Edited by MG2R
Link to comment
Share on other sites

Link to post
Share on other sites

<redacted>

 

edit: actually i think i misunderstood your question

you can't write the condition like that, you have to break it into "x = 0 and y = 0"

Ohh yeah that's how the operator works. It should be if (x = 0 & y = 0) and not if (x&y = 0) LOL my bad

 

Edit: Yup exactly as you edited it :D

MacBook Pro 15' 2018 (Pretty much the only system I use)

Link to comment
Share on other sites

Link to post
Share on other sites

I was wondering about a shorter way of doing a statement like this, and a possiblity could also be
 

if( x == y == 0 ){}

Aragorn (WS): 250D | 6800k | 840 Pro 512GB | Intel 530 480GB  | Asus X99-M WS | 64GB DDR4 | Corsair HX720i | GTX 1070 | Corsair H115i | Philips BDM4350UC 43" 3840x2160 IPS

Gimli (server):  Node 304 | G4560 | ADATA XPG SX8000 128GB | 2x 5TB WD Red | ASROCK H270M-ITX/AC  | 8GB DDR4 | Seasonic 400FL

 Omega (server):                 Fractal Arc Mini R2 | i3 4130 | 500GB Maxtor | 2TB WD Red : Raid 1 | 3TB Seagate Barracuda | 16GB RAM | Seasonic G-450w
Alpha (WS): 900D | 4770k | GTX 780  | 840 Pro 512GB  | GA-Z87X-OC | Corsair RM 850 | 24GB 2400mhz | Samsung S27B970D 2560x1440

                              ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Link to comment
Share on other sites

Link to post
Share on other sites

 

I was wondering about a shorter way of doing a statement like this, and a possiblity could also be

 

if( x == y == 0 ){}

but y==0 would return a truth value if y=0, which won't equal to x if x = 0

also, i believe it's just a type missmatch (int == bool)

Link to comment
Share on other sites

Link to post
Share on other sites

but y==0 would return a truth value if y=0, which won't equal to x if x = 0

also, i believe it's just a type missmatch (int == bool)

Maybe if (x=y == 0) could work?

MacBook Pro 15' 2018 (Pretty much the only system I use)

Link to comment
Share on other sites

Link to post
Share on other sites

but y==0 would return a truth value if y=0, which won't equal to x if x = 0

also, i believe it's just a type missmatch (int == bool)

Oh yeah :P I'm such a boob :P

Aragorn (WS): 250D | 6800k | 840 Pro 512GB | Intel 530 480GB  | Asus X99-M WS | 64GB DDR4 | Corsair HX720i | GTX 1070 | Corsair H115i | Philips BDM4350UC 43" 3840x2160 IPS

Gimli (server):  Node 304 | G4560 | ADATA XPG SX8000 128GB | 2x 5TB WD Red | ASROCK H270M-ITX/AC  | 8GB DDR4 | Seasonic 400FL

 Omega (server):                 Fractal Arc Mini R2 | i3 4130 | 500GB Maxtor | 2TB WD Red : Raid 1 | 3TB Seagate Barracuda | 16GB RAM | Seasonic G-450w
Alpha (WS): 900D | 4770k | GTX 780  | 840 Pro 512GB  | GA-Z87X-OC | Corsair RM 850 | 24GB 2400mhz | Samsung S27B970D 2560x1440

                              ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Link to comment
Share on other sites

Link to post
Share on other sites

Maybe if (x=y == 0) could work?

nope, that would compare y to 0, and try to assing the result to x

 

Urm... You kinda can write a condition like that...  :mellow:

what he was trying to write is

if(x & y == 0)

which can't be written 'naturally' with just one comparison operator

Link to comment
Share on other sites

Link to post
Share on other sites

edit: nvm

i think that

x | y == 0

would do the trick, but yeah, it's nothing you would ever use in a code that has to be readable

Link to comment
Share on other sites

Link to post
Share on other sites

i think that

x | y == 0

would do the trick, but yeah, it's nothing you would ever use in a code that has to be readable

I was actually considering saying that (it only works for == 0 though)....actually in java it is

(x|y) == 0 //so even less readable

I was also going to mention though that this is a likely a bad way since it is less readable and slower....the only reason you beat me to this though is I don't believe the results I am seeing at the moment...so I will post the section of code that is bothering me...because I am getting that this method is 4x faster when all the results are true, 3x-4x faster when x >= 1, with y = 0, and 6x faster when x = 0 and y = 1.......I give up, anyone know if Java is really that slow at internal operations (I am guessing because it abstracts everything, except for bitwise it forces near assembly level compilation)...any actual reasons for this result would be kindly appreciated (or if I am messing up my test code).

//p,y,i,cnt were created somewhere else and I also swapped the if statements and still got the same results        p = 0; y = 1; i = 0; cnt = 0;//start clock here after initializing				for(i=0; i < 100000000; i++) {			if((p|y) == 0) {				cnt++;			}		}//output here		        p = 0; y = 1; i = 0; cnt = 0;//Start clock here after intializing the variables				for(i=0; i < 100000000; i++) {			if(p == 0 && y == 0) {				cnt++;			}		}//Output time

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

-snip-

i just tried the same code in C: the OR method is slightly (not significantly) faster

edit: cut out some nonsense

Link to comment
Share on other sites

Link to post
Share on other sites

public class TestMe {    public static void main(String [] args){        test(0,0);        test(0,0);        test(0,1);        test(0,1000);        test(1,0);        test(12323,0);        test(123123,4234234);        test(0,0);        test(0,0);    }    static void test(int x, int y){        long stop = 1000*1000*1000;        long count = 0;        long count2 = 0;        long t1,t2,t3;        t1 = System.currentTimeMillis();        for(long i=0; i < stop; i++){            if((x|y)==0) count++;        }        t2 = System.currentTimeMillis();        for(long i=0; i < stop; i++){            if(x == 0 && y == 0) count2++;        }        t3 = System.currentTimeMillis();        System.out.println("----------");        System.out.println("x: " + x + " - y: " + y);        System.out.println(t2-t1 + "ms (x|y)==0");        System.out.println(t3-t2+"ms (x==0&&y==0)");    }}

Output:

----------x: 0 - y: 0428ms (x|y)==01672ms (x==0&&y==0)----------x: 0 - y: 0396ms (x|y)==01708ms (x==0&&y==0)----------x: 0 - y: 1277ms (x|y)==0279ms (x==0&&y==0)----------x: 0 - y: 1000328ms (x|y)==0277ms (x==0&&y==0)----------x: 1 - y: 0284ms (x|y)==0277ms (x==0&&y==0)----------x: 12323 - y: 0278ms (x|y)==0279ms (x==0&&y==0)----------x: 123123 - y: 4234234277ms (x|y)==0281ms (x==0&&y==0)----------x: 0 - y: 0371ms (x|y)==0555ms (x==0&&y==0)----------x: 0 - y: 0372ms (x|y)==0562ms (x==0&&y==0)

Mini-Desktop: NCASE M1 Build Log
Mini-Server: M350 Build Log

Link to comment
Share on other sites

Link to post
Share on other sites

public class TestMe {    public static void main(String [] args){        test(0,0);        test(0,0);        test(0,1);        test(0,1000);        test(1,0);        test(12323,0);        test(123123,4234234);        test(0,0);        test(0,0);    }    static void test(int x, int y){        long stop = 1000*1000*1000;        long count = 0;        long count2 = 0;        long t1,t2,t3;        t1 = System.currentTimeMillis();        for(long i=0; i < stop; i++){            if((x|y)==0) count++;        }        t2 = System.currentTimeMillis();        for(long i=0; i < stop; i++){            if(x == 0 && y == 0) count2++;        }        t3 = System.currentTimeMillis();        System.out.println("----------");        System.out.println("x: " + x + " - y: " + y);        System.out.println(t2-t1 + "ms (x|y)==0");        System.out.println(t3-t2+"ms (x==0&&y==0)");    }}

Output:

----------x: 0 - y: 0428ms (x|y)==01672ms (x==0&&y==0)----------x: 0 - y: 0396ms (x|y)==01708ms (x==0&&y==0)----------x: 0 - y: 1277ms (x|y)==0279ms (x==0&&y==0)----------x: 0 - y: 1000328ms (x|y)==0277ms (x==0&&y==0)----------x: 1 - y: 0284ms (x|y)==0277ms (x==0&&y==0)----------x: 12323 - y: 0278ms (x|y)==0279ms (x==0&&y==0)----------x: 123123 - y: 4234234277ms (x|y)==0281ms (x==0&&y==0)----------x: 0 - y: 0371ms (x|y)==0555ms (x==0&&y==0)----------x: 0 - y: 0372ms (x|y)==0562ms (x==0&&y==0)

Wow that's a pretty big difference in speed! Nice find!

MacBook Pro 15' 2018 (Pretty much the only system I use)

Link to comment
Share on other sites

Link to post
Share on other sites

Wow that's a pretty big difference in speed! Nice find!

 

It almost seems to be like that ....

 

BUT:

public class TestMe {    public static void main(String [] args){        //test(0,0);        int stop = 1000;        long stop2 = 1000*1000*1000;        long total1=0, total2=0;        int x,y;        for(int i=0; i<stop;i++){            x = (int)(Math.random()*2);            y = (int)(Math.random()*2);            x *= (int)(Math.random()*Integer.MAX_VALUE);            y *= (int)(Math.random()*Integer.MAX_VALUE);            total1 += test1(x,y,stop2);            total2 += test2(x,y,stop2);        }        System.out.println(total1/stop+"ms for |");        System.out.println(total2/stop+"ms for &&");    }    static long test1(int x, int y, long stop){        long count = 0;        long t1,t2;        t1 = System.currentTimeMillis();        for(long i=0; i < stop; i++){            if((x|y)==0) count++;        }        t2 = System.currentTimeMillis();        //System.out.println("|: "+x+" "+y+" - "+(t2-t1));        return t2-t1;    }    static long test2(int x, int y, long stop){        long count = 0;        long t1,t2;        t1 = System.currentTimeMillis();        for(long i=0; i < stop; i++){            if(x == 0 && y == 0) count++;        }        t2 = System.currentTimeMillis();        //System.out.println("&: "+x+" "+y+" - "+(t2-t1));        return t2-t1;    }}
301ms for |301ms for &&Process finished with exit code 0

edit:

25% x==0 and y==0

25% x!=0 and y==0

25% x==0 and y!=0

25% x!=0 and y!=0

Mini-Desktop: NCASE M1 Build Log
Mini-Server: M350 Build Log

Link to comment
Share on other sites

Link to post
Share on other sites

-snip-

i edited your code like this to represent a more 'realistic' situation:

import java.util.Random;public class TestMe {    static int[] v;    static final int size = 100000;    public static void main(String [] args){        fill();        warmup();        test();    }    static void fill(){        Random r = new Random();        float chanceOfZero = .2f;        v = new int[size];        for(int i = 0; i < size; i++){            int val = r.nextInt();            if(val < Integer.MAX_VALUE * chanceOfZero) val = 0;            v[i] = val;        }    }    static void warmup(){        int x = 2, y = 3;        long count = 0;         for(long j=0; j < 10000; j++)            for(int i=1; i < size; i++){                if((v[i]|v[i-1])==0) count++;                if(v[i] == 0 && v[i-1] == 0) count++;            }    }    static void test(){        long stop = 10 * 1000;        long count = 0;        long t1,t2,t3;        t1 = System.nanoTime();        for(long j = 0; j < stop; j++)            for(int i = 1; i < size; i++)                if((v[i] | v[i-1]) == 0)                    count++;        t2 = System.nanoTime();        for(long j = 0; j < stop; j++)            for(int i = 1; i < size; i++)                if(v[i] == 0 || v[i-1] == 0)                    count++;        t3 = System.nanoTime();        System.out.println("----------");        System.out.println(((t2-t1)/1000000) + "ms (x|y)==0");        System.out.println(((t3-t2)/1000000)+"ms (x==0&&y==0)");    }}

 

the interesting thing is to tweak the chance of the arrays elements to be zero:

with chanceOfZero = 99%

864ms (x|y)==0592ms (x==0&&y==0)

 

with chanceOfZero = 90%

1305ms (x|y)==0901ms (x==0&&y==0)

with chanceOfZero = 70%

1157ms (x|y)==01599ms (x==0&&y==0)

 

with chanceOfZero = 30%

1159ms (x|y)==02944ms (x==0&&y==0)

 

with chanceOfZero = .01%

1164ms (x|y)==03562ms (x==0&&y==0)

 

these results look weird to me, but maybe the ==&&== method is making use of a short-circuit evaluation which turns out to be suboptimal in this particular case, since the second evaluation is so cheap

Link to comment
Share on other sites

Link to post
Share on other sites

 

these results look weird to me, but maybe the ==&&== method is making use of a short-circuit evaluation which turns out to be suboptimal in this particular case, since the second evaluation is so cheap

 

if x doesn't equal zero, then && never even touches y

 

edit: nvm

Mini-Desktop: NCASE M1 Build Log
Mini-Server: M350 Build Log

Link to comment
Share on other sites

Link to post
Share on other sites

if x doesn't equal zero, then && never even touches y

exactly, but to do that i presume that it has to do a jump or something, which is more expensive than evaluating the second expression

Link to comment
Share on other sites

Link to post
Share on other sites

exactly, but to do that i presume that it has to do a jump or something, which is more expensive than evaluating the second expression

 

I just reread what you said, you are probably right.

Mini-Desktop: NCASE M1 Build Log
Mini-Server: M350 Build Log

Link to comment
Share on other sites

Link to post
Share on other sites

I guess the thing to take out of this is, it will all depend on the type of evaluation you are doing.  If you know that the likelyhood of a match is true, | is better.  In the event one variable is more likely than the other, then && might be better (I have a feeling that 1, vs max int might have different costs with | and && I can't test it now, I might test it later though if I remember)....after all for 1 if it compares left to right then it does a lot more comparisons for 0, but max int would only need 1 bit check.

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×