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: *compile 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, wherepcis a pointer to the current character:Anyway, as you can see, it’s a pretty tight loop that does
isdigitorishexdigitortoloweronce 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.