switch x if else condition

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 605 Views - Last Post: 04 October 2012 - 12:22 PM Rate Topic: -----

#1 Dworza  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 172
  • Joined: 13-February 11

switch x if else condition

Posted 03 October 2012 - 04:05 AM

Hello...I was wondering, what is better to use... Can somebody please explain me, how does switch for strings in java 7 works? Is it better to use switch or if else in loop with many cycles (for saving memory and time)?
IE:
SWITCH STATEMENT:
 String name = "John"
 switch (name) {
            case "Peter": 
                     //do something
                     break;
            case "Ron": 
                     //do something
                     break;
            case "Martin": 
                     //do something
                     break;
            case "Steve":  
                     //do something
                     break;
            case "John":
                     //do something
                     break;
            default: 
                     //do something
                     break;
        } 


IF-ELSE:
 String name = "John"
 if(name.equals("Peter") {
                     //do something
                     }
 else if (name.equals("Ron") {
                     //do something
                     }
 else if (name.equals("Martin") {
                     //do something
                     }
 else if (name.equals("Steve") {
                     //do something
                     }
 else if (name.equals("Johon") {
                     //do something
                     }
 else  //do something



Is This A Good Question/Topic? 0
  • +

Replies To: switch x if else condition

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1373
  • View blog
  • Posts: 3,025
  • Joined: 05-April 11

Re: switch x if else condition

Posted 03 October 2012 - 05:33 AM

I believe the switch gets compiled to the if-else part you just wrote :)
Was This Post Helpful? 1
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10384
  • View blog
  • Posts: 38,434
  • Joined: 27-December 08

Re: switch x if else condition

Posted 03 October 2012 - 05:39 AM

Just being anal here, but don't forget the parentheses at the end of your if statements. :)
Was This Post Helpful? 0
  • +
  • -

#4 Dworza  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 172
  • Joined: 13-February 11

Re: switch x if else condition

Posted 03 October 2012 - 07:28 AM

@macosxnerd101...yeah, you're right...I just copy-pasted the code and didn't notice, that I dind't copy the right parenthese :)
Was This Post Helpful? 0
  • +
  • -

#5 Kakerergodt  Icon User is offline

  • D.I.C Head

Reputation: 87
  • View blog
  • Posts: 201
  • Joined: 01-May 12

Re: switch x if else condition

Posted 03 October 2012 - 07:57 AM

Dunno what the difference is, but I tested it by running it in a loop 2000000000 times, the switch-case code used 2000ms and the if/else code used 8ms.
Was This Post Helpful? 0
  • +
  • -

#6 Dworza  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 172
  • Joined: 13-February 11

Re: switch x if else condition

Posted 04 October 2012 - 02:52 AM

Uh that's weird...I got different results.. :plain: Here is my code:
package pkg;

public class Main {

	public static void main(String[] args) {	
		long i;
		long elapsedTime ;
		long start = System.nanoTime();
		long loops =  2000000000;
		System.out.println(loops + " loops");
		
		for (i = 0; i != loops ; i++) {
			String name = "John";
			switch (name) {
			case "Peter":
				// do something
				break;
			case "Ron":
				// do something
				break;
			case "Martin":
				// do something
				break;
			case "Steve":
				// do something
				break;
			case "John":
				// do something
				break;
			default:
				// do something
				break;
			}
		}
		
		elapsedTime = System.nanoTime() - start;
		System.out.println(elapsedTime + " ms SWITCH");

		start = System.nanoTime();
		for (i = 0; i != loops ; i++) {
			String name = "John";
			if (name.equals("Peter")) {
				// do something
			} else if (name.equals("Ron")) {
				// do something
			} else if (name.equals("Martin")) {
				// do something
			} else if (name.equals("Steve")) {
				// do something
			} else if (name.equals("Johon")) {
				// do something
			} else
				continue;// do something
		}
		
		elapsedTime = System.nanoTime() - start;
		System.out.println(elapsedTime + " ms IF-ELSE");
	}
}



and here are the results:
2000 loops
1706362 ms SWITCH
431340 ms IF-ELSE

200000 loops
8615899 ms SWITCH
7556547 ms IF-ELSE

20000000 loops
386069458 ms SWITCH
685789675 ms IF-ELSE

200000000 loops
3695497891 ms SWITCH
6867239272 ms IF-ELSE

300000000 loops
5417970440 ms SWITCH
10308872217 ms IF-ELSE

2000000000 loops
38295357092 ms SWITCH
69228458645 ms IF-ELSE


This post has been edited by Dworza: 04 October 2012 - 02:52 AM

Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: switch x if else condition

Posted 04 October 2012 - 05:08 AM

View PostKakerergodt, on 03 October 2012 - 10:57 AM, said:

Dunno what the difference is, but I tested it by running it in a loop 2000000000 times, the switch-case code used 2000ms and the if/else code used 8ms.

You ran your test using == or equals() in the if ?
Was This Post Helpful? 0
  • +
  • -

#8 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1373
  • View blog
  • Posts: 3,025
  • Joined: 05-April 11

Re: switch x if else condition

Posted 04 October 2012 - 05:12 AM

When I tested with String, the switch took about double the time of the if-else when doing many loops

Then I switch to checking an int, and they were about equal :)
I don't know what the switch on Strings get decompiled to, but on my computer it wasn't as fast

	public static void main(String[] args) {
		int numb = 1365464;
		long loops = 200000000;
		long end = 0;
		long start = System.nanoTime();
		
		for (int i=0; i<loops; i++) {
			if (numb == 64324) {
				
			}
			else if (numb == 84762) {
				
			}
			else if (numb == 1365464) {

			}
			else {
				
			}
		}
		
		end = System.nanoTime();
		System.out.println("If-else: " + (end - start));
		
		end = 0;
		start = System.nanoTime();
		for (int i=0; i<loops; i++) {
			switch (numb) {
				case 64324:
					break;
				case 84762:
					break;
				case 1365464:
					break;
				default:
					break;
			}
		}
		
		end = System.nanoTime();
		System.out.println("Switch: " + (end - start));
	}


Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: switch x if else condition

Posted 04 October 2012 - 05:15 AM

View PostDworza, on 04 October 2012 - 05:52 AM, said:

Uh that's weird...I got different results.. :plain: Here is my code:
package pkg;

public class Main {

	public static void main(String[] args) {	
		long i;
		long elapsedTime ;
		long start = System.nanoTime();
		long loops =  2000000000;
		System.out.println(loops + " loops");


When you do tests like that be carefull where you put your I/Os they will scrw up everything
here even wort, the println() time is included after nanoTime()

You should store the benchmark results in an array and print the time at the end

  int NB_TEST = 10;
  long[] start = new long[NB_TEST];
  long[] end = new long[NB_TEST};

  for(int i = 0; i < NB_TEST; ++i) {
     start[i] = System...
     ... run the test loop
     end[i] = System...
  }
  // all test done print result



And CasiOo is right, switch are just for nice display purpose, ther are translated by if/else by the compiler
Was This Post Helpful? 1
  • +
  • -

#10 Dworza  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 172
  • Joined: 13-February 11

Re: switch x if else condition

Posted 04 October 2012 - 05:53 AM

@pbl ah ok...you're right..thanks for explanation :)
Was This Post Helpful? 0
  • +
  • -

#11 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2651
  • View blog
  • Posts: 11,182
  • Joined: 20-September 08

Re: switch x if else condition

Posted 04 October 2012 - 08:42 AM

Quote

And CasiOo is right, switch are just for nice display purpose, ther are translated by if/else by the compiler


Actually that's not necessarily the case. It could easily be compiled to a lookupswitch/tableswitch instruction

This post has been edited by g00se: 04 October 2012 - 09:28 AM
Reason for edit:: typo

Was This Post Helpful? 1
  • +
  • -

#12 Kakerergodt  Icon User is offline

  • D.I.C Head

Reputation: 87
  • View blog
  • Posts: 201
  • Joined: 01-May 12

Re: switch x if else condition

Posted 04 October 2012 - 09:34 AM

View Postpbl, on 04 October 2012 - 05:08 AM, said:

View PostKakerergodt, on 03 October 2012 - 10:57 AM, said:

Dunno what the difference is, but I tested it by running it in a loop 2000000000 times, the switch-case code used 2000ms and the if/else code used 8ms.

You ran your test using == or equals() in the if ?


This is the code I ran:
public static void ifElse()
{
	String name = "John";
	long time = System.nanoTime();

	for(int i = 0; i < 2000000000; i++)
	{
		if(name.equals("Peter"))
		{}
		else if(name.equals("Ron"))
		{}
		else if(name.equals("Martin"))
		{}
		else if(name.equals("Steve"))
		{}
		else if(name.equals("John"))
		{}
		else{}
	}
	time = System.nanoTime() - time;
	long tid = time/1000000L;
	System.out.println("IfElse: " + tid + " ms");
}

public static void switchCase()
{
	String name = "John";
	long time = System.nanoTime();

	for(int i = 0; i < 2000000000; i++)
	{
		switch (name)
		{
			case "Peter":
						break;
			case "Ron":
						break;
			case "Martin":
						break;
			case "Steve":
						break;
			case "John":
						break;
			default:
						break;
		}
	}
	time = System.nanoTime() - time;
	long tid = time/1000000L;
	System.out.println("SwitchCase: " + tid + " ms");
}


It outputs:
IfElse: 6 ms
SwitchCase: 1439 ms



Edit: When I added an instruction(j++) to all of the cases and all of the else if's except the one that was true(ie. not in case/elseif "John"), the results were more similar:
IfElse: 4363 ms. j = 0
SwitchCase: 5684 ms. j = 0

This post has been edited by Kakerergodt: 04 October 2012 - 09:39 AM

Was This Post Helpful? 0
  • +
  • -

#13 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,694
  • Joined: 19-March 11

Re: switch x if else condition

Posted 04 October 2012 - 09:50 AM

View Postpbl, on 04 October 2012 - 07:15 AM, said:

And CasiOo is right, switch are just for nice display purpose, ther are translated by if/else by the compiler


This is not actually the case. The switch compiles into a tableswitch instruction in the jvm - basically a jump table - while the if version would compile to serious of comparisons.

So the switch is more efficient in the jvm than the ifs. I can post some jvm code if anyone is curious about it.

However, and I say this all the time, if you're just doing a data lookup a map is a much better solution. It's more flexible, it's faster, and it's cleaner code. Command objects are a nuisance, so there's a case for using Switch if you're deciding on actions, but depending on what happens with the lambdas in 8, we might lose the need for switch entirely.


EDIT: I see Goose got there before I did - Goose is right

This post has been edited by jon.kiparsky: 04 October 2012 - 09:51 AM

Was This Post Helpful? 0
  • +
  • -

#14 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,694
  • Joined: 19-March 11

Re: switch x if else condition

Posted 04 October 2012 - 10:04 AM

View PostKakerergodt, on 04 October 2012 - 11:34 AM, said:

Edit: When I added an instruction(j++) to all of the cases and all of the else if's except the one that was true(ie. not in case/elseif "John"), the results were more similar:
IfElse: 4363 ms. j = 0
SwitchCase: 5684 ms. j = 0


This is not surprising. The compiler can do some optimizations. Always have some code in your test branches!
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: switch x if else condition

Posted 04 October 2012 - 10:54 AM

Quite easy to verify if a switch() is converted to if/else or not
Write a Java program with one
Compile it
Reverse compile it with a DeCompiler
see if the recosntructed code uses if/else or a switch
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2