Results 1 to 10 of 10
  1. #1
    tcms is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default switch to If-Else test

    Hi,

    I made a switch method, and now I need to test the performance of the "same" method but with If-Else.

    I know the result will be: Switch is faster, but I need to make test's to prove it.

    I need the exactly same code in if-else structure

    Cheers
    Last edited by tcms; 05-19-2013 at 08:26 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default Re: switch to If-Else test

    I didn't know enums with numerical 'names' were allowed in Java ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    tcms is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Re: switch to If-Else test

    And they aren't, I just posted like this to make easier to understand what I want

  4. #4
    tcms is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Re: switch to If-Else test

    I made something like this, but I'am getting "the final field cannot be assigned)

    Java Code:
    public void test() {
    		if (this == key0) {
    			PerformanceTest.key0 = key1;
    		} else if (this == key1) {
    			PerformanceTest.key1 = key0;
    		} else if (this == key2) {
    			PerformanceTest.key2 = key3;
    		} else if (this == key3) {
    			PerformanceTest.key3 = key2;
    		} else
    			PerformanceTest.key1 = key1;
    
    	}
    Last edited by tcms; 05-19-2013 at 07:52 PM.

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,924
    Rep Power
    4

    Default Re: switch to If-Else test

    Why not try again and use the correct syntax for your enum.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    tcms is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Re: switch to If-Else test

    Quote Originally Posted by jim829 View Post
    Why not try again and use the correct syntax for your enum.

    Regards,
    Jim
    Done, I edited the post's
    Last edited by tcms; 05-19-2013 at 08:07 PM.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default Re: switch to If-Else test

    Quote Originally Posted by tcms View Post
    Done, I edited the post's
    Did you try to compile it? And? Was the compiler happy?

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,924
    Rep Power
    4

    Default Re: switch to If-Else test

    You are close. But this syntax is not correct.

    Java Code:
    public void test() {
    
            if (this == key0) {
                PerformanceTest.key0 = key1;
            } else if (this == key1) {
                PerformanceTest.key1 = key0;
            } else if (this == key2) {
                PerformanceTest.key2 = key3;
            } else if (this == key3) {
                PerformanceTest.key3 = key2;
            } else
                PerformanceTest.key1 = key1;
    }

    Try changing the signature from using void to PerformanceTest and then just return the desired key. That would be a more fair test anyway.

    Edit: Just to explain my original statement, enum type fields are implicitly final. Hence the syntax error in trying to reassign them.

    Regards,
    Jim
    Last edited by jim829; 05-19-2013 at 08:23 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    tcms is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Re: switch to If-Else test

    Oh you're right!

    I made it like this:

    Java Code:
    public PerformanceTest test() {
     
            if (this == key0) {
                return key1;
            } else if (this == key1) {
                return key0;
            } else if (this == key2) {
                return key3;
            } else if (this == key3) {
                return key2;
            } else
                return key1;
    }

  10. #10
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,924
    Rep Power
    4

    Default Re: switch to If-Else test

    Yes. To me, the real difference is returning a value at the end, ether immediately or after properly assigning it to a return value. I would think the generated byte code would fall thru the next if/else statements once a true condition was met.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Switch
    By KillerToFu_xX in forum New To Java
    Replies: 2
    Last Post: 03-19-2013, 11:20 AM
  2. Switch
    By java4amanda in forum New To Java
    Replies: 13
    Last Post: 03-21-2012, 09:53 AM
  3. switch
    By dj kourampies in forum New To Java
    Replies: 17
    Last Post: 01-30-2009, 05:32 PM
  4. switch
    By dj kourampies in forum New To Java
    Replies: 2
    Last Post: 01-30-2009, 08:46 AM
  5. Replies: 0
    Last Post: 04-05-2008, 12:38 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
  •