Quote:
Originally Posted by
cleopard
This might be a silly question, but I thought I'd ask anyway. If I'm writing in C, isn't it more efficient to, for instance, use constant character variable set to 'A' instead of hard-coding a character 'A'? Since it's only a single character instead of a string, it might not matter much.
Whether it's more efficient depends on the compiler and architecture. If you wanted to find out which is which, you could generate code to execute each code 100000 times, and then see which is more efficient.
Unter Intel architecture, hard-coding is more efficient because the compiler generates machine code in which the character @ is actually embedded into the instruction (implicit data). With the constant character, the compiler might also do this, or it might generate machine code using direct (absolute address) or indirect (relative to the stack) data addressing, both of which are less efficient. However, due to code caching and such, it's very unlikely to make any difference, unless this code is in many different places throughout the program.
Just for kicks, I wrote such programs and here are the run-times. When they finish, I'll post the run-times.