KOFKS said:
I'm trying to do some image processing work, and the result is
floating point number. Using "float" type will comsume less memory,
while lots of functions in math.h and some other libs return a
"double" type. So I want to know whether the operator promotion from
"float" to "double", "int" to "float“ or "int" to "double" cause
efficiency loss. When does operator promotion happen? preprocessing,
compiling or running?
I try to get information from FAQ or searching here, but get
inadequate explaination.
That's probably the case because "operator promotion" is a meaningless
phrase, so you won't be able to find it any FAQ. My best guess is that
you are asking about type conversions, and the following answer is based
upon that guess:
All type conversions that actually do anything take a certain amount of
time, and therefore cost you some efficiency. An example of a type
conversion that don't actually do anything would be conversion from int
to long, on a platform where int and long have the same representation.
However, those costs are usually negligible. It's usually far more
important to consider other issues, such as the amount of space required
by the new type, the amount of precision it provides, the range of
values it can represent, and the speed with which operations in that
type are executed.
Type conversion can occur during preprocessing, but the only place it
can occur is in #if conditions, where the only possible types are
intmax_t, and uintmax_t, so the only possible conversion are between
those two types. Furthermore, since casts are not permitted, the only
conversions that occur are implicit.
The standard does not impose any rigid distinctions between what can be
done at compile time and what can be done at run time. It talks about
"translation" of a program and "execution" of a program, but gives
implementors a lot of freedom about how they actually implement those
processes.
Constant expressions (including those which involve type conversions)
can be evaluated at compile time, and usually are. The main exception is
the evaluation of floating point expressions, which many compilers will
defer until runtime, particularly if compiled on a machine which might
have a different FPU than the one where the code will be running.
However, unless it occurs in context where a integer constant expression
is required, any constant expression can have it's evaluation deferred
until run time.
Evaluation of non-constant expressions (including those that involve
type conversion) can usually only occur at runtime, because they depend
upon inputs provided to the program which are not known at compile time.
However, that is not always an issue. If, for instance, you write a
function that takes no input, calculates the value of pi using a series
expansion, and returns that value, a compiler is permitted to, in
effect, execute that function at compile time, determine the value it
would return, and replace all calls to that function with the value it
would have returned. Any type conversions that occurred in such code
would occur at compile time.