Results 1 to 6 of 6

Thread: Is switch or if and else more memory efficient

  1. #1
    Join Date
    Dec 2009
    Location
    North Carolina
    Posts
    71
    Thanks
    13
    Thanked 3 Times in 3 Posts

    Default Is switch or if and else more memory efficient

    The application would be you have a number between 1 and 3 and depending on how many numbers there are that changes what the output is. Would it be more efficient to use switch or if else if and else?

    Tim
    Last edited by twQ; 01-04-2010 at 05:04 PM.

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Use switch, if anything I think it should take less time to load.
    Jeremy | jfein.net

  3. The Following User Says Thank You to Nile For This Useful Post:

    twQ (01-04-2010)

  4. #3
    Join Date
    Dec 2009
    Location
    North Carolina
    Posts
    71
    Thanks
    13
    Thanked 3 Times in 3 Posts

    Default

    Thats what I've been using it was more curiosity. Thanks Nile.

    Tim

  5. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Glad to help! When I said I think it takes less time, please note that I, as a programmer, don't care if the page takes 10 more mili seconds then it should.
    Jeremy | jfein.net

  6. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Realistically I doubt that it will matter. You can test it by doing something like 1000 iterations of a loop with it, then compare the results.
    However, this is going to be more of an issue for much longer sets. The use of switch statements is when you have too many items to easily setup if/else statements, where you'd end up inside the 50th else-- at that point it's a much better idea for your sanity if nothing else to use the more "flat" switch statements. They may run faster, but I really have no idea what is behind them-- for all I know it may just be a shortcut the writing the equivalent if/else statements, though probably not since it is a basic element of the language.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #6
    Join Date
    Dec 2009
    Location
    North Carolina
    Posts
    71
    Thanks
    13
    Thanked 3 Times in 3 Posts

    Default

    Yeah I see what you mean. I use switches because I find them easier to read in the code sometimes. Other times if/else makes more sense.

    BTW Nile every time I read your name I think of this death metal band named Nile hahaha.

    Thanks for the answers, I doubted it would make much to any difference but in the future this stuff could be a big deal (probably not but maybe).

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
  •