Note that there are some explanatory texts on larger screens.

plurals
  1. POCase vs If Else If: Which is more efficient?
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicates:</strong><br> <a href="https://stackoverflow.com/questions/767821/is-else-if-faster-than-switch-case">is &ldquo;else if&rdquo; faster than &ldquo;switch() case&rdquo; ?</a><br> <a href="https://stackoverflow.com/questions/2086529/what-is-the-relative-performance-of-if-else-vs-switch-in-java">What is the relative performance of if/else vs. switch in Java?</a> </p> </blockquote> <p>Ive been coding-in-the-run again....when the debugger steps through a case statement it jumps to the item that matches the conditions immediately, however when the same logic is specified using if/else it steps through every if statement until it finds the winner. Is the case statement more efficient, or is my debugger just optimizing the step through? (don't worry about the syntax/errors, i typed this in SO, don't know if it will compile, its the principle i'm after, I didn't want to do them as ints cause i vaguely remember something about case using an offset with ints) I use C#, but im interested in a general answer across programming languages.</p> <pre><code>switch(myObject.GetType()){ case typeof(Car): //do something break; case typeof(Bike): //do something break; case typeof(Unicycle): //do something break; case default: break; } </code></pre> <p><strong>VS</strong></p> <pre><code> Type myType = myObject.GetType(); if (myType == typeof(Car)){ //do something } else if (myType == typeof(Bike)){ //do something } else if (myType == typeof(Unicycle)){ //do something } else{ } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload