Thread: Java Optimization/Boolean Logic Test

Page 2 of 4 FirstFirst 1234 LastLast
Results 11 to 20 of 32
  1. #11  
    SERGEANT OF THE MASTER SERGEANTS MOST IMPORTANT PERSON OF EXTREME SERGEANTS TO THE MAX!

    cube's Avatar
    Join Date
    Jun 2007
    Posts
    8,871
    Thanks given
    1,854
    Thanks received
    4,745
    Rep Power
    5000
    Quote Originally Posted by Killer 99 View Post
    i, j, k, l
    a, b, c, d, e, f, g, h, i
    Reply With Quote  
     

  2. #12  
    Banned

    Join Date
    Mar 2008
    Posts
    2,595
    Thanks given
    128
    Thanks received
    191
    Rep Power
    0
    Quote Originally Posted by Mister Maggot View Post
    Benchmark it, lol.

    Lines of code are irrelevant; it's still put the same into bytecode.
    in that case it is relevant since ternary doesn't produce same bytecode (but it's not speed relevant :trollface
    Reply With Quote  
     

  3. #13  
    Registered Member
    AlexMason's Avatar
    Join Date
    Aug 2007
    Age
    30
    Posts
    1,199
    Thanks given
    17
    Thanks received
    27
    Rep Power
    98
    To even benchmark this properly you need to save the results and find a median.

    Reply With Quote  
     

  4. #14  
    Registered Member
    Mister Maggot's Avatar
    Join Date
    Dec 2008
    Posts
    7,227
    Thanks given
    3,283
    Thanks received
    2,875
    Rep Power
    5000
    Quote Originally Posted by filth View Post
    in that case it is relevant since ternary doesn't produce same bytecode (but it's not speed relevant :trollface
    java - What "if" is faster - classic or shorthand? - Stack Overflow

    Gf.
    Reply With Quote  
     

  5. #15  
    Banned

    Join Date
    Mar 2008
    Posts
    2,595
    Thanks given
    128
    Thanks received
    191
    Rep Power
    0
    you know what I'm not even gonna take this opportunity to make you feel bad since I don't hate you but you do realize that link you provided completely agrees with what I said right?
    Reply With Quote  
     

  6. Thankful user:


  7. #16  
    Registered Member Killer 99's Avatar
    Join Date
    Dec 2007
    Posts
    1,480
    Thanks given
    171
    Thanks received
    503
    Rep Power
    414
    Once again the second method is the least of your problems...
    Reply With Quote  
     

  8. #17  
    Registered Member
    Mister Maggot's Avatar
    Join Date
    Dec 2008
    Posts
    7,227
    Thanks given
    3,283
    Thanks received
    2,875
    Rep Power
    5000
    1: 342
    2: 684

    Code:
    public class Main {
    
        public static void main(String[] args) {
            method1();
            method2();
        }
    
        public static void method1() {
            long n = System.nanoTime();
            int x = 1;
            if(true)
                x = 2;
            long s = System.nanoTime();
            System.out.println("1: " + (s - n));
        }
    
        public static void method2() {
            long n = System.nanoTime();
            int x = true == true? 1 : 2;
            long s = System.nanoTime();
            System.out.println("2: " + (s - n));
        }
    }
    Reply With Quote  
     

  9. #18  
    Registered Member
    Mister Maggot's Avatar
    Join Date
    Dec 2008
    Posts
    7,227
    Thanks given
    3,283
    Thanks received
    2,875
    Rep Power
    5000
    Code:
    public class Main {
    
        private static int ITERATIONS = 200000;
    
        public static void main(String[] args) {
            method1();
            method2();
        }
    
        public static void method1() {
            long n = System.nanoTime();
            for(int i = 0; i < ITERATIONS; ++i) {
                int x = 1;
                if(true)
                    x = 2;
            }
            long s = System.nanoTime();
            System.out.println("1: " + (s - n) + " avg:" + ((s - n) / ITERATIONS));
        }
    
        public static void method2() {
            long n = System.nanoTime();
            for(int i = 0; i < ITERATIONS; ++i) {
                int x = true == true? 1 : 2;
            }
            long s = System.nanoTime();
            System.out.println("2: " + (s - n) + " avg:" + ((s - n) / ITERATIONS));
        }
    }
    Code:
    public class Main extends java.lang.Object{
    public Main();
      Code:
       0:   aload_0
       1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
       4:   return
    
    public static void main(java.lang.String[]);
      Code:
       0:   invokestatic    #2; //Method method1:()V
       3:   invokestatic    #3; //Method method2:()V
       6:   return
    
    public static void method1();
      Code:
       0:   invokestatic    #4; //Method java/lang/System.nanoTime:()J
       3:   lstore_0
       4:   iconst_0
       5:   istore_2
       6:   iload_2
       7:   getstatic       #5; //Field ITERATIONS:I
       10:  if_icmpge       23
       13:  iconst_1
       14:  istore_3
       15:  iconst_2
       16:  istore_3
       17:  iinc    2, 1
       20:  goto    6
       23:  invokestatic    #4; //Method java/lang/System.nanoTime:()J
       26:  lstore_2
       27:  getstatic       #6; //Field java/lang/System.out:Ljava/io/PrintStream;
       30:  new     #7; //class java/lang/StringBuilder
       33:  dup
       34:  invokespecial   #8; //Method java/lang/StringBuilder."<init>":()V
       37:  ldc     #9; //String 1:
       39:  invokevirtual   #10; //Method java/lang/StringBuilder.append:(Ljava/lang
    /String;)Ljava/lang/StringBuilder;
       42:  lload_2
       43:  lload_0
       44:  lsub
       45:  invokevirtual   #11; //Method java/lang/StringBuilder.append:(J)Ljava/la
    ng/StringBuilder;
       48:  ldc     #12; //String  avg:
       50:  invokevirtual   #10; //Method java/lang/StringBuilder.append:(Ljava/lang
    /String;)Ljava/lang/StringBuilder;
       53:  lload_2
       54:  lload_0
       55:  lsub
       56:  getstatic       #5; //Field ITERATIONS:I
       59:  i2l
       60:  ldiv
       61:  invokevirtual   #11; //Method java/lang/StringBuilder.append:(J)Ljava/la
    ng/StringBuilder;
       64:  invokevirtual   #13; //Method java/lang/StringBuilder.toString:()Ljava/l
    ang/String;
       67:  invokevirtual   #14; //Method java/io/PrintStream.println:(Ljava/lang/St
    ring;)V
       70:  return
    
    public static void method2();
      Code:
       0:   invokestatic    #4; //Method java/lang/System.nanoTime:()J
       3:   lstore_0
       4:   iconst_0
       5:   istore_2
       6:   iload_2
       7:   getstatic       #5; //Field ITERATIONS:I
       10:  if_icmpge       21
       13:  iconst_1
       14:  istore_3
       15:  iinc    2, 1
       18:  goto    6
       21:  invokestatic    #4; //Method java/lang/System.nanoTime:()J
       24:  lstore_2
       25:  getstatic       #6; //Field java/lang/System.out:Ljava/io/PrintStream;
       28:  new     #7; //class java/lang/StringBuilder
       31:  dup
       32:  invokespecial   #8; //Method java/lang/StringBuilder."<init>":()V
       35:  ldc     #15; //String 2:
       37:  invokevirtual   #10; //Method java/lang/StringBuilder.append:(Ljava/lang
    /String;)Ljava/lang/StringBuilder;
       40:  lload_2
       41:  lload_0
       42:  lsub
       43:  invokevirtual   #11; //Method java/lang/StringBuilder.append:(J)Ljava/la
    ng/StringBuilder;
       46:  ldc     #12; //String  avg:
       48:  invokevirtual   #10; //Method java/lang/StringBuilder.append:(Ljava/lang
    /String;)Ljava/lang/StringBuilder;
       51:  lload_2
       52:  lload_0
       53:  lsub
       54:  getstatic       #5; //Field ITERATIONS:I
       57:  i2l
       58:  ldiv
       59:  invokevirtual   #11; //Method java/lang/StringBuilder.append:(J)Ljava/la
    ng/StringBuilder;
       62:  invokevirtual   #13; //Method java/lang/StringBuilder.toString:()Ljava/l
    ang/String;
       65:  invokevirtual   #14; //Method java/io/PrintStream.println:(Ljava/lang/St
    ring;)V
       68:  return
    
    static {};
      Code:
       0:   ldc     #16; //int 200000
       2:   putstatic       #5; //Field ITERATIONS:I
       5:   return
    
    }
    1: 630641 avg:3
    2: 778122 avg:3
    Reply With Quote  
     

  10. #19  
    Banned

    Join Date
    Mar 2008
    Posts
    2,595
    Thanks given
    128
    Thanks received
    191
    Rep Power
    0
    Maggot, you're comparing if statements against ternary which is not the same as if-else obviously.

    And also in your second test, that's not a proper way to use ternary it's better to just use an if statement

    Just quit it, I am 100% right lol, compare the byte code of if-else and ternary when you are setting the value of a variable. Ternary will be 1 statement less BUT they will end up doing the same thing which is why the speed remains the same, it's just that with if-else (two statement) it returns on both conditions (or continues to the next part of the method) as opposed to ternary where it sets the value of the statement after evaluating the boolean and THEN returns/continues.
    Reply With Quote  
     

  11. #20  
    Renown Programmer
    veer's Avatar
    Join Date
    Nov 2007
    Posts
    3,746
    Thanks given
    354
    Thanks received
    1,370
    Rep Power
    3032
    you pasted bytecode but did you care to look at it? the code isn't even functionally the same. besides, the compiler isn't a fucking retard; the expressions are simplified at compile time to the source code equivalents of:
    Code:
    int x = 1; x = 2;
    (jeez no dead code optimizations?)
    Code:
    int x = 1;
    Reply With Quote  
     

  12. Thankful user:


Page 2 of 4 FirstFirst 1234 LastLast

Thread Information
Users Browsing this Thread

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


User Tag List

Similar Threads

  1. [Test Your Java Skills] Major flaw in PI
    By Debugger in forum Help
    Replies: 34
    Last Post: 12-03-2010, 03: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
  •