Thread: Java Optimization/Boolean Logic Test

Page 1 of 4 123 ... LastLast
Results 1 to 10 of 32
  1. #1 Java Optimization/Boolean Logic Test 
    Registered Member
    Killer 99's Avatar
    Join Date
    Dec 2007
    Posts
    1,455
    Thanks given
    167
    Thanks received
    500
    Rep Power
    400
    Fastest optimization (or implementation) wins, you can use this to test your Boolean logic skills in Java. I will post my solution when its all done, good luck!

    Code:
    	public boolean a(boolean b, boolean c, boolean d, boolean e)
    	{
    		if (b && d)
    			e = false;
    
    		if (b)
    		{
    			if (e && d)
    				b = true;
    
    			while (c)
    			{
    				if (b)
    					c = e;
    				else
    					b = d;
    
    				if (b)
    					if (e)
    						e = false;
    					else
    						b = e;
    
    
    				if (d)
    				{
    					if (e)
    					{
    						if (d)
    							d = c;
    						else
    							d = b;
    
    						e = d;
    					}
    					if (d && e)
    					{
    						c = e;
    						e = false;
    						if (c)
    							d = true;
    						else
    							d = false;
    
    					}
    				}
    				if (e || d)
    				{
    					e = false;
    					if (b)
    						e = true;
    
    					b = false;
    					if (e)
    						d = b;
    
    					int h = f(e, d);
    					if (h == 2)
    					{
    						if (!d && e)
    							h = 0;
    
    					}
    					if (h == 3)
    					{
    						if (c)
    							e = d;
    						else
    							e = c;
    
    					}
    					else if (h == 4)
    					{
    						if (e)
    							c = e;
    						else
    							c = d;
    
    					}
    					else if (h == 1)
    					{
    						if (b)
    							h += h;
    
    						if (e)
    							h -= h;
    
    						if (h == 2)
    							b = c;
    
    					}
    					if (h == 0)
    					{
    						for (int g = 0; g < h; g++)
    						{
    							boolean i = c;
    							b = c;
    							d = b;
    							if (g == 1)
    								c = e;
    							else
    								c = i;
    
    						}
    
    						if (h == 0)
    							if (c)
    								e = b;
    							else
    								c = e;
    
    
    						if (e)
    							d = false;
    
    					}
    				}
    				else
    				{
    					if (b)
    						b = e;
    					else
    						b = c;
    
    					if (c)
    						c = b;
    
    					if (b)
    						b = c;
    
    					int j = f(c, b);
    					if (j == 1 || j == 3)
    						e = d;
    
    				}
    			}
    		}
    		if (c && b)
    		{
    			d = e;
    			e = false;
    		}
    		return b;
    	}
    
    	public int f(boolean a, boolean b)
    	{
    		if (!a && !b)
    			return 0;
    
    		if (!a && b)
    			return 1;
    
    		if (a && !b)
    			return 2;
    
    		if (a && b)
    			return 3;
    
    		return 4;
    	}
    Reply With Quote  
     

  2. #2  
    Registered Member

    Join Date
    Jan 2008
    Age
    21
    Posts
    1,387
    Thanks given
    76
    Thanks received
    385
    Rep Power
    962
    Holy fuck the naming.
    Reply With Quote  
     

  3. Thankful user:


  4. #3  
    I'm smarter then you

    Mister Maggot's Avatar
    Join Date
    Dec 2008
    Age
    18
    Posts
    7,100
    Thanks given
    3,259
    Thanks received
    2,870
    Rep Power
    5000
    He thinks he's cool cause he writes like an obfuscator.
    Quote Originally Posted by Original View Post
    Yeah, I haven't used JFrame before, I've always used OpenGL
    Quote Originally Posted by Harlan View Post
    well c++ compiler is annoying as heck and can't be trusted
    I coded a boolean once
    \_(ツ)_/ lol idk just rep me
    Behemoth RSPS: https://github.com/mistermaggot/behemoth i have no idea what i am doing
    66.176.193.75 -- Hack me; I triple dog dare you.
    [Today 11:29 PM] MOB KLICK: Syria would blow the USA to pieces that's why you didn't evade
    Reply With Quote  
     

  5. Thankful users:


  6. #4  
    RuneHub

    Benji's Avatar
    Join Date
    Feb 2010
    Age
    20
    Posts
    1,529
    Thanks given
    925
    Thanks received
    505
    Rep Power
    529
    I did the second one,

    Code:
    public int f(boolean a, boolean b) {
    	if(a) 
    		return if(b) ? 3 : 2;
    	else
    		return if(b) ? 1 : 0;
    }
    6 lines instead of 12.

    I'm doing the first one, looks like a bitch
    "Sincerity is an overrated virtue"

    Milton Friedman
    Reply With Quote  
     

  7. #5  
    I'm smarter then you

    Mister Maggot's Avatar
    Join Date
    Dec 2008
    Age
    18
    Posts
    7,100
    Thanks given
    3,259
    Thanks received
    2,870
    Rep Power
    5000
    Benchmark it, lol.

    Lines of code are irrelevant; it's still put the same into bytecode.
    Quote Originally Posted by Original View Post
    Yeah, I haven't used JFrame before, I've always used OpenGL
    Quote Originally Posted by Harlan View Post
    well c++ compiler is annoying as heck and can't be trusted
    I coded a boolean once
    \_(ツ)_/ lol idk just rep me
    Behemoth RSPS: https://github.com/mistermaggot/behemoth i have no idea what i am doing
    66.176.193.75 -- Hack me; I triple dog dare you.
    [Today 11:29 PM] MOB KLICK: Syria would blow the USA to pieces that's why you didn't evade
    Reply With Quote  
     

  8. #6  
    Doctor p - Sweet Shop


    Join Date
    Apr 2007
    Age
    21
    Posts
    6,821
    Thanks given
    142
    Thanks received
    587
    Rep Power
    2355
    Quote Originally Posted by Benjii View Post
    I did the second one,

    Code:
    public int f(boolean a, boolean b) {
    	if(a) 
    		return if(b) ? 3 : 2;
    	else
    		return if(b) ? 1 : 0;
    }
    6 lines instead of 12.

    I'm doing the first one, looks like a bitch
    Lol that wouldn't even work.

    And if you really don't want many lines then:

    Code:
    public int f(boolean a, boolean b) {
    		return a ? (b ? 3 : 2) : (b ? 1 : 0);
    	}
    But as said before, amount of lines don't matter.
    Reply With Quote  
     

  9. #7  
    RuneHub

    Benji's Avatar
    Join Date
    Feb 2010
    Age
    20
    Posts
    1,529
    Thanks given
    925
    Thanks received
    505
    Rep Power
    529
    Your right, mine should be

    Code:
    public int f(boolean a, boolean b) {
    	if(a) 
    		return b ? 3 : 2;
    	else
    		return b ? 1 : 0;
    }
    and your right again, trying to get it down to however many lines doesn't improve performance
    "Sincerity is an overrated virtue"

    Milton Friedman
    Reply With Quote  
     

  10. #8  
    Banned
    Join Date
    Dec 2010
    Posts
    13
    Thanks given
    0
    Thanks received
    0
    Rep Power
    0
    Warmup...
    (Joe) Test 0...Took 7785.214
    (Me ) Test 1...Took 4165.532
    Code:
    import static java.lang.System.*;
    
    public class NewMain {
    
        public static void main(String[] args) {
    
            int tests = 100000000;
            boolean b0 = false, b1 = true;
    
            out.println("Warmup...");
            for (int i = 0; i < tests; i++) {
    
                for (int j = 0; j < 3; j++) {
                    b0 = !b0;
                    for (int l = 0; l < 3; l++) {
                        b1 = !b1;
                        test1(b0, b1);
                        test0(b0, b1);
                    }
                }
            }
    
            long start;
    
            out.print("Test 0...");
            start = System.nanoTime();
            for (int i = 0; i < tests; i++) {
    
                for (int j = 0; j < 3; j++) {
                    b0 = !b0;
                    for (int l = 0; l < 3; l++) {
                        b1 = !b1;
                        test0(b0, b1);
                    }
                }
            }
            out.println("Took " + (System.nanoTime() - start) / 1000000F);
    
            out.print("Test 1...");
            start = System.nanoTime();
            for (int i = 0; i < tests; i++) {
    
                for (int j = 0; j < 3; j++) {
                    b0 = !b0;
                    for (int l = 0; l < 3; l++) {
                        b1 = !b1;
                        test1(b0, b1);
                    }
                }
            }
            out.println("Took " + (System.nanoTime() - start) / 1000000F);
        }
    
        public static final int test1(boolean a, boolean b) {
    
            if (a) {
                if (b) {
                    return 3;
                }
                return 2;
    
            } else if (b) {
                return 1;
            }
            return 0;
        }
    
        public static final int test0(boolean a, boolean b) {
            if (!a && !b) {
                return 0;
            }
    
            if (!a && b) {
                return 1;
            }
    
            if (a && !b) {
                return 2;
            }
    
            if (a && b) {
                return 3;
            }
    
            return 4;
        }
    }
    Reply With Quote  
     

  11. #9  
    Registered Member
    Killer 99's Avatar
    Join Date
    Dec 2007
    Posts
    1,455
    Thanks given
    167
    Thanks received
    500
    Rep Power
    400
    You know if I where you I would be focusing on the first method, lol.
    Reply With Quote  
     

  12. #10  
    Registered Member
    Killer 99's Avatar
    Join Date
    Dec 2007
    Posts
    1,455
    Thanks given
    167
    Thanks received
    500
    Rep Power
    400
    Quote Originally Posted by Col1337 View Post
    Code:
    import static java.lang.System.*;
    
    public class NewMain {
    
        public static void main(String[] args) {
    
            int tests = 100000000;
            boolean b0 = false, b1 = true;
    
            out.println("Warmup...");
            for (int i = 0; i < tests; i++) {
    
                for (int j = 0; j < 3; j++) {
                    b0 = !b0;
                    for (int l = 0; l < 3; l++) {
                        b1 = !b1;
                        test1(b0, b1);
                        test0(b0, b1);
                    }
                }
            }
    
            long start;
    
            out.print("Test 0...");
            start = System.nanoTime();
            for (int i = 0; i < tests; i++) {
    
                for (int j = 0; j < 3; j++) {
                    b0 = !b0;
                    for (int l = 0; l < 3; l++) {
                        b1 = !b1;
                        test0(b0, b1);
                    }
                }
            }
            out.println("Took " + (System.nanoTime() - start) / 1000000F);
    
            out.print("Test 1...");
            start = System.nanoTime();
            for (int i = 0; i < tests; i++) {
    
                for (int j = 0; j < 3; j++) {
                    b0 = !b0;
                    for (int l = 0; l < 3; l++) {
                        b1 = !b1;
                        test1(b0, b1);
                    }
                }
            }
            out.println("Took " + (System.nanoTime() - start) / 1000000F);
        }
    
        public static final int test1(boolean a, boolean b) {
    
            if (a) {
                if (b) {
                    return 3;
                }
                return 2;
    
            } else if (b) {
                return 1;
            }
            return 0;
        }
    
        public static final int test0(boolean a, boolean b) {
            if (!a && !b) {
                return 0;
            }
    
            if (!a && b) {
                return 1;
            }
    
            if (a && !b) {
                return 2;
            }
    
            if (a && b) {
                return 3;
            }
    
            return 4;
        }
    }
    i, j, k, l
    Reply With Quote  
     

  13. Thankful user:


Page 1 of 4 123 ... LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [Test Your Java Skills] Major flaw in PI
    By The Duck in forum Help
    Replies: 34
    Last Post: 12-03-2010, 02:10 AM
  2. Player updating optimization.
    By Maxi in forum Snippets
    Replies: 6
    Last Post: 04-03-2010, 10:44 PM
  3. Runescape Text Optimization.
    By bloodargon in forum Snippets
    Replies: 7
    Last Post: 09-26-2009, 04:09 AM
  4. Server Java Knowledge test.
    By Colby in forum RS2 Server
    Replies: 5
    Last Post: 06-10-2009, 08:25 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •