CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 3 of 3 FirstFirst 123
Results 31 to 35 of 35
  1. #31
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: switch case or if else ?

    "Also, on some platforms, bit fields are packed left-to-right, on others right-to-left "
    Yes, that's why I went to some length in discussing endianess. But the OP is using Windows and MS VS and Windows always uses little endian. If code using bit fields is ported to a non-Windows big endian OS then obviously there would be issues to be addressed.

    The only reliable way to pull apart an encoded stream from some external source is to use << >> & | ^ as appropriate.
    You still need to take endianess into account when dealing with externally sourced binary data.
    Last edited by 2kaud; August 22nd, 2020 at 04:10 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  2. #32
    Join Date
    May 2015
    Posts
    500

    Re: switch case or if else ?

    Thanks a lot everybody for your valuable comments and inputs. Very much appreciated.

    @wolle: The switch case (of if else ) is needed, just for efficiency. But otherwise, both values will work for default case. But when the value is 0x01, we know all elements are 1. (so to avoid bit shift and or operations, i felt it is better to separate out that case ) ?

  3. #33
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: switch case or if else ?

    But when the value is 0x01, we know all elements are 1.

    ?? value of 0x01 means that only the LSB (bit 0) is set (1). The rest of the bits are unset (0).
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #34
    Join Date
    May 2015
    Posts
    500

    Re: switch case or if else ?

    @kaud: Sorry for not being very clear, when input is exactly 0x0001, the output elements are:
    i.e
    usRank1elt = 1;
    usRank2elt = 1;
    usRank4elt = 1;
    usRank8elt = 1;
    usRank16elt = 1;
    usRank32elt = 1;

    and no need to do the bitshift operations.

  5. #35
    Join Date
    Feb 2017
    Posts
    677

    Re: switch case or if else ?

    Quote Originally Posted by pdk5 View Post
    @wolle: The switch case (of if else ) is needed, just for efficiency.
    Are you sure? As I argued in #30, the straight code alternative I suggested may actually be more efficient.

    1. First, say it stands between using the if statement or the switch statement. Since there is just one selection the if is optimal and will be faster for sure. The switch may be equally fast but only if the compiler turns it into the equivalent of an if, otherwise it will be slower. In my view to avoid this uncertainty it's better to use if rather than switch.

    2. But say you skip both if and switch and instead use the straight code I showed in #30. It means the expressions will always be performed in full also in the special case when the value is 0x01. The question is how probable is 0x01? If it has a low probability of appearing the other case will dominate in the long run. At some specific probability threshold the cost of the if statement outweighs the gain of the special case and the straight code alternative is faster.

    3. Now without the if or switch selection the code has become branch-free. In general, removing branches makes your code faster because it reduces miss-predictions,

    https://lemire.me/blog/2019/10/15/mi...running-times/

    4. Furthermore, I forgot to mention in #30 why I slightly modified your code. If you look at the usRank variable in your code you see it's updated between the assignments whereas in mine it's not, it's constant throughout (and can be declared const where it's defined). The advantage is that then the compiler is free to lay down code that executes the assignments in parallel which is faster than sequentially.

    ---

    With so much going for it, I would seriously consider the straight code alternative. It may very well be the fastest option and in addition it's simpler and thus easier to understand and maintain.
    Last edited by wolle; September 5th, 2020 at 12:44 AM.

Page 3 of 3 FirstFirst 123

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured