Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When code optimization happens?

Yesterday, I had an interview. There they asked me when the code optimization happens? Say,

int abc;//Global variable
abc = 3;
if(abc == 3)
{
  printf("abc will be always 3");
}
else
{
  printf("This will never executed");
}

Now the question is when the optimization happens? A... At Run time B... At compile time. I answered at compile time... To me I thought, compiler checks for volatile keyword at compile time. If the variable is not declared as volatile then it optimizes the code. But, when the compiler comes to know that, this variable is never ever going to be other than 3? If it is at run-time, then when compiler comes to know that variable is never ever going to be other than 3? Because if the variable is going to be changed after this part of the code executed. Please clear my doubt

like image 669
Rasmi Ranjan Nayak Avatar asked Feb 04 '12 16:02

Rasmi Ranjan Nayak


People also ask

What happens during code optimization?

The code optimization in the synthesis phase is a program transformation technique, which tries to improve the intermediate code by making it consume fewer resources (i.e. CPU, Memory) so that faster-running machine code will result.

Why do we use code optimization?

Code optimization is any method of code modification to improve code quality and efficiency. A program may be optimized so that it becomes a smaller size, consumes less memory, executes more rapidly, or performs fewer input/output operations.

Who is responsible for code optimization?

Code optimization is responsibility of : Application programmer.

Is code optimization an important phase of compilation?

Code optimization is an important phase to improve the time and space requirement of the generated target code. Given a code in intermediate form it applies optimizing techniques and reduces the code size and returns the code in intermediate form.


2 Answers

C code is usually compiled using static (aka ahead-of-time) compilation. The code is set in stone once it leaves the compiler; it cannot change at runtime.

This is in contrast to languages that use just-in-time compilation, such as Java. There, optimizations can happen pretty much at any time while then program is running.

like image 147
NPE Avatar answered Sep 21 '22 23:09

NPE


Most code optimization can't happen at runtime, at least not how you mean. The code can't change during execution to reflect a new or different set of variables, that would just make an absolute mess.

What can be done at runtime is choosing the best path through the code, but that has to be done mostly manually in order to create the separate paths, early outs, branches and so on to allow optimization.

Code like this is, as written, allows compile-time optimization because the compiler can check for any possible alternate values of abc and, if none are found, optimize out the call. The scope of the search greatly influences whether that can happen, however, and the compiler settings influence that.

If the compiler is simply naively optimizing single object files and your second line is in another file from the print section, then it may not be able to guarantee abc doesn't change, and so won't be able to optimize this at all. Regardless of variable use, it depends on how aggressive your compiler settings are and whether they are allowed to discard dead branches, or will consider doing so. Optimizing for size may be more likely to remove the branch than for speed, but medium/high settings will likely do it either way (if possible).

Most modern compilers have a whole-program-optimization option, which delays much of the optimization until the linker stage. This allows it to search the whole program, potentially discover that abc is never changed or used anywhere else, and remove the variable and failing branch from the condition. Whole program optimization can be far more effective than separate optimization for each object, because it can allow more accurate searching.

In the case where it's not possible for the compiler to trim dead code, you can either hint it (recent constructs such as constexpr can help with that) or add optimizations yourself. It could be as simple as putting the most-likely path first and including a return before the else, saving the CPU from doing a jump. That sort of micro-optimization is unlikely to be necessary, certainly not in a simple example like this, where the if is plenty optimization by itself.

like image 27
ssube Avatar answered Sep 19 '22 23:09

ssube