Notices
Results 1 to 5 of 5

Thread: Does one if-else statement take more processing power or a switch statement with two cases?

  1. #1 Does one if-else statement take more processing power or a switch statement with two cases? 
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    911
    if-else example:
    Code:
    bool myBoolean(true);
    if(myBoolean)
    cout<<"hi"<<endl;
    else
    cout<<"bye"<<endl;
    switch-2case example:
    Code:
    bool myBoolean(true);
    switch(myBoolean){
    case true:
    cout<<"hi"<<endl;
    break;
    case false:
    cout<<"bye"<<endl;
    }
    specifically for the more popular c++ compilers, which takes less computing cycles, and why?

    also, should my last case have a corresponding break in high-standard c++ coding standards?
    how about default cases? should they have a corresponding break too?

    Will compilers use more time to process a break? (assuming its the last thing you type in a switch argument)


    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  2.  
     

  3. #2  
    Forum Radioactive Isotope MagiMaster's Avatar
    Join Date
    Jul 2006
    Posts
    3,440
    This is a micro-optimization question, which I'm going to say upfront is the wrong way to approach optimization. And in this case it will almost certainly make no difference at all. Modern compilers are quite good at optimizing things and these two will give the same machine code.

    The break on the last case does nothing and will compile to nothing, but it might be good to add it anyway for the sake of your own consistency. In general, breaks will take the same amount of time as any non-conditional branch, which is on the order of a couple CPU cycles (which isn't even a billionth of a second on a few GHz machine).

    Condition branches take a little longer, but the compiler already does everything it can to work around that and you'll only hamper it by writing convoluted code that it can't figure out.


    Reply With Quote  
     

  4. #3  
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    911
    Thanks for the reply, and yeah, I understand its a micro-optimization question that may make hardly any difference in the software.

    But I'm trying to implement a binary tree search to search through a huge database in the shortest time.

    My lecturer said that micro-optimization will make a difference when I'm trying to search through a single mega-sized database (which may not even be sorted) with millions of people trying to search for stuff in it at any one time.

    So "these two will give the same machine code. "?
    What do u mean by "machine code"?
    Do you mean the flow of how the logic gates will be used?
    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  5. #4  
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    911
    Also, I have a question regarding enumeration.

    How do I define an enumeration that can contain up to a certain number, e.g. 0 to 255?

    I checked Other data types - C++ Tutorials
    , then i used
    Code:
    enum class tileInfo:unsigned char{
            T_NONE = 0,
            T_three,
            t_fo,
            T_MAX = 258
        };

    Shouldn't this enum only be able to hold up to 255?

    How come it's not giving me any error compiling?

    Is there a way to limit the max enum number?

    Or did I use the syntax wrongly?

    EDIT:
    i tried doing
    Code:
    T_MAX = 9999
    and still no compile error
    Code:
        cout<<(unsigned char)CMap::tileInfo::T_MAX<<endl;
    out came some funny symbol, but then i tried 9998 and a different symbol came up again

    EDIT2:
    now, i've experimented with the T_MAX, and realized that when i checked with this table

    I found out that when i set T_MAX to some number above Dec256, the compiler automatically deducts Dec256(which i guess is the scope for unsigned char) from the the Dec i set, it goes back to Dec0.
    Last edited by RamenNoodles; July 19th, 2014 at 09:40 AM.
    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  6. #5  
    Forum Radioactive Isotope MagiMaster's Avatar
    Join Date
    Jul 2006
    Posts
    3,440
    A brief aside on how compilers work. The code you write in C++ is not what gets run by your computer. If it was, you wouldn't need a compiler at all. You could just double-click on your code file and run it directly. Instead, the compiler takes your code and then translates it (though several intermediaries) to machine code, the native binary language of the computer. It makes plenty of optimizations and subtle changes along the way (though hopefully without changing the intent in the process). Modern compilers are very good at this.

    And your lecturer is wrong, at least in the general sense. Micro-optimizations will never help in general for (at least) two reasons. First, like I said, the compiler is already very good at making optimizations for itself, and most micro-optimizations don't do anything it's not already doing. In the case of your two examples, by the time it's done compiling, they'll look exactly the same. Second, people are very bad judges of what really needs to be optimized. If you are not profiling your code, you really don't know what is taking so long. (That said, you should still put as little code as possible in your inner most loop. The simple fact that it's run the most often means it's a good place to focus.) BTW, profiling means to time parts of your code so that you can figure out where it's spending all its time.

    As for the enums, I don't use that particular feature of them much, but I suspect C++ does what it does with every other integer type. It just quietly overflows when you give it too big of a number. An unsigned char can only hold 8 bits (0 to 255), so when you give it a number, it only uses the lowest 8 bits of that number and ignores the rest. You can work out what the result would be by taking the number mod 256, or cast it to an integer and print it out. I don't think there's any way to force it to treat overflows as a compiler or runtime error, though I might be wrong about that.
    Reply With Quote  
     

Similar Threads

  1. difference between switch statement and vector table compilation
    By sciencestudy in forum Computer Science
    Replies: 1
    Last Post: December 31st, 2013, 08:08 AM
  2. My Indroductory Statement
    By OshadhaTheKnowing in forum Introductions
    Replies: 1
    Last Post: August 20th, 2012, 08:30 AM
  3. Statement for our Future
    By Schizo in forum Politics
    Replies: 6
    Last Post: August 12th, 2008, 09:15 AM
  4. Acer statement
    By FancyShoes in forum Computer Science
    Replies: 7
    Last Post: April 5th, 2007, 12:20 AM
  5. Goring's statement
    By icewendigo in forum History
    Replies: 8
    Last Post: July 12th, 2006, 10:07 PM
Bookmarks
Bookmarks
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •