Craig Dickson wrote:
Paul Scott wrote:
I doubt if this is your problem but on most compilers:
double num;
num = 4;
is inefficient; It normally causes an integer constant 4 to be stored
somewhere. Then when num = 4; is executed the integer 4 is converted
to double every you execute the code.
That might have been true of C compilers 20 years ago, but I doubt it's
true today. gcc is probably smart enough to realize that when a literal
constant being assigned to a variable, that the constant should be of
the same type as the variable.
Well that may date me a little even though I am actively programming at
this moment. I will research this a little more. My logic would be it
would break the rules of the language to assume that conversion. When
would the compiler know to follow the programmers intention if it
changed the definition of operations like = . I'm not saying you are
wrong for gcc but it's an interesting question.
Similarly, it isn't true anymore that "while (1)" is less efficient than
"for (;;)", which, 20 years ago, was true on some compilers; for "while
(1)", they would actually generate a silly test like, "Load a register
with 1, compare the register to zero, jump if equal" -- which,
obviously, always failed. I haven't seen that in a long, long time.
This example could be quite different since it doesn't involve the
standard conversion rules.
Thanks for commenting,
Paul