开发者

Hexadecimal vs decimal on binary conversion

开发者 https://www.devze.com 2023-03-22 14:33 出处:网络
For example, here are two ways to set an integer variable (say C++): int x = 0xFF; int y = 255; Which statement would compile faster to set the actual bits to the integer value?

For example, here are two ways to set an integer variable (say C++):

int x = 0xFF;
int y = 255;

Which statement would compile faster to set the actual bits to the integer value?

Edited: *compi开发者_运维技巧le changed from execute. I assumed the conversion to binary was at execution time, but seems to be at compile time given @muntoo's answer


There is absolutely no difference in execution time, and probably no difference in compilation speed.

Added in response to comment:
Here's what happens. The compiler has a parser, probably recursive-descent, and at bottom it calls a lexer to get tokens. In this case the token after = is a number, which it can tell by the leading digit, so it goes something like this, where pc is a pointer to the current character:

if (isdigit(*pc)){
  intpart = 0;
  if (pc[0]=='0' && pc[1]=='x'){
    pc += 2;
    while(ishexdigit(*pc)){
      intpart *= 16;
      if (isdigit(*pc)){
        intpart += (*pc - '0')
      }
      else {
        intpart += (tolower(*pc) - 'a' + 10);
      }
      pc++;
    }
  }
  else {
    while(isdigit(*pc)){
      intpart *= 10;
      intpart += (*pc - '0');
      pc++;
    }
    if (*pc == '.'){
      // ... handle fractional part
    }
  }
}

Anyway, as you can see, it's a pretty tight loop that does isdigit or ishexdigit or tolower once or twice for each character in the number, and multiplication, subtraction, and addition. Assuming those functions are in-line, we're talking maybe 10-20 instructions per character. It's maybe slightly more instructions per character in the hex case, but the decimal number will have somewhat more characters, so it's hard to tell a-priori which should be faster. This only happens for the total number of such integers you had the energy to type in your code, like maybe around a 100. If the machine can do, say, 10^8 instructions per second, it can read digits at the rate of around 10^7 per second, or around 100 nanoseconds per character, or 10 microseconds for all the numbers in your file, give or take an order of magnitude.

Once the compiler knows it's a number, it just becomes part of the abstract syntax tree, which is used to generate assembly language. By that time, the fact that it was hexadecimal or decimal has long since been forgotten. All it knows is it's a binary integer of a certain value, so the assembly language will be identical.


0xFF is the same as 255, in the point of view of your compiler. Both will generate exactly the same code. Your compiler "converts" them both to 11111111b.


Compilation speed all depends on the compiler and how it compiles.

For example 0xFF could be identified as a hexadecimal number (ignoring regexes, which would be the preferred method):

bool myishex(string sz)
{
    if(sz[0] == '0' && sz[1] == 'x')
    {
        for(size_t i = 2; i < sz.length(); ++i)
            if(!(sz[i] >= '0' && sz[i] <= '9') || !(sz[i] >= 'A' && sz[i] <= 'F') || !(sz[i] >= 'a' && sz[i] <= 'f'))
                return(false);
    }
    else
    {
        return(false);
    }

    return(true);
}

Compared to myisnum():

bool myisnum(string sz)
{
    for(size_t i = 0; i < sz.length(); ++i)
        if(!(sz[i] >= '0' && sz[i] <= '9'))
            return(false);

    return(true);
}

Well, myisnum() is generally faster than myishex(). (The probability of A-F or a-f is higher than 0-9.)


But conversion from FF to a binary number may be faster slower than 255.

int myhex2bin(string sz)
{
    int b = 0;

    sz = sz.substr(2); // Cut the "0x" out.

    for(size_t i = 0; i < sz.length(); ++i)
    {
        sz[i] = tolower(sz[i]);
        b += myhexdigit2dec(sz[i]) << ((sz.length() - (i + 1)) << 2);
    }

    return(b);
}

// Unsafe. Optimized for speed.
int myhexdigit2dec(char c)
{
    return(c < 'A' ? c - '0' : (c < 'a' ? c - 'A' + 10 : c - 'a' + 10));
}

Whereas you don't get no bitshifts in decimal to binary, but you don't get no A-F nor a-f neither.

int mydec2bin(string sz)
{
    int b = 0;
    int factor = 1;

    for(size_t i = sz.length(); i > 0; --i)
    {
        b += (sz[i - 1] - '0') * factor;
        factor *= 10;
    }

    return(b);    
}

Conclusion: Still depends on the compiler, but the 255 is probably faster to compile. :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号