Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

to use if else or switch  RSS feed

 
Raja Sagar Panamgipalli
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,
I have this code that runs to several thousands of lines and the only branching or constructs that were ever used are
if[conditions]
-------------
--statements-
else
-------------
--statements-

and sometimes

if(!(condition))
-------------
--statements-

All i wanna know is,can there be any better performance gain if i use switch() statements if so how better?

Thank Q
Sagar
 
Dmitry Melnik
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could the condition change during execution of those if-statements and pieces of code controlled by them?

Is the condition always a check for equality of integers (or similar types)?
 
Raja Sagar Panamgipalli
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Melnik..

No the conditions dont change ...it jus that i want to restructure entire code to give some face lift performance wise...
Which do u think is more efficient...

thank Q...
 
Raja Sagar Panamgipalli
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Melnik..
The conditions are like....the following...

(smthng.equalsIgnoreCase(smthng)
or..(smthng.equals(smthng)

ThanQ...
 
Dmitry Melnik
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and values of both arguments of equalsIgnoreCase(...) or equals(...) never change during the process, right?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The switch statement can not be used with Strings.

If you *really* know that this code is a performance bottleneck (have you profiled it?), you could use a combination of the Command pattern and a HashSet to retrieve the Command for a specific String.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and:
if(!(condition))
can't be translated into a switch-statement too.
 
Dmitry Melnik
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It can. Use your fantasy, and think out of the box for 2 minutes

But even if it can, it does not mean that it should
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
simplest case:


Like Dmitry said, it can be done but should it
 
Raja Sagar Panamgipalli
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wenting thanx Man,

The code which currently exists can be tranformed into switch from its current if else structure.
I only feel clarity and understandability of the control flow is gained in doing so rather than performance.

Thanx 4 the info guys,...

Sagar
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Three switch statements.
I'm deeply impressed!
 
Bruno Dery
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I remember right switch statements can be executed in one processor cycle while if - else take as much cycles as there are if - elses. Anybody else remembers that bit of info (for me it goes all the way back to school days )
 
Dmitry Melnik
Ranch Hand
Posts: 328
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
switch statements can be executed in one processor cycle while if - else take as much cycles as there are if - elses.

Your statement may be true or false depending on the following factors:
a). processor and target instruction set
b). language
c). compiler
d). set of compiler options (like target code optimization)

Speaking about Java (which is platform independent) -- one can never be sure
Plus, the JVM will add quite a few processor cycles of its own to each Java statement. The number of cycles added depends on the platform and JVM implementation.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

needs only one cycle too, if a < 7.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!