×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Regular Contributor
    Join Date
    Mar 2003
    Location
    Rome, Italy
    Posts
    53

    performance question...

    it's better a solution like this

    switch(number)
    { case 0:
    case 1:
    case 2:
    }

    ...or with if...else if... else if??
    thanks a lot!!

  2. #2
    Regular Contributor
    Join Date
    Mar 2003
    Location
    Tampere / Finland
    Posts
    201

    RE: performance question...

    Hello,

    For my personal opinion, switch-case has a better performance but I haven't investigated more about this topic.

    Regards,
    Kui

  3. #3
    Registered User
    Join Date
    Mar 2003
    Location
    Hampshire, England
    Posts
    9

    RE: performance question...

    You will probably get a slight improvement out of the switch-case statement, but it's not going to be too noticeable unless you do it very very often. On the other side of the coin, the repetitive "if (x==whatever)" statements will JAR up smaller when compiled due to the repeating pattern of code.

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    38

    RE: performance question...

    Hi all.
    I personally would not suggest to use a series of If/else-if statements instead of "switch-case" to optimize your code. Most of times the "switch" statement is slightly faster (even if your data types are automatically promoted to ints) especially if the numbers are close together (uses a fast direct lookup).
    Moreover I would not sacrifice the readability and the "constrains" of a good switch statement in most cases (gameStatus, level, ...) and I keep using if-else where the statements are more complex.
    I've frankly always found that substitution a messy hassle.

    regards
    davide
    <AtomicTag.com/>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×