Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is /optimize C# compiler key intended for?

Is there a full list of optimizations done by the /optimize C# compiler key available anywhere?

EDIT: Why is it disabled by default? Is it worth using in a real-world app? -- it is disabled by default only in Debug configuration and Enabled in Release.

like image 450
Max Galkin Avatar asked Jun 09 '09 10:06

Max Galkin


People also ask

What is optimization in C?

Optimization is a program transformation technique, which tries to improve the code by making it consume less resources (i.e. CPU, Memory) and deliver high speed. In optimization, high-level general programming constructs are replaced by very efficient low-level programming codes.

What is optimize in coding?

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.

What does an optimizing compiler do?

In computing, an optimizing compiler is a compiler that tries to minimize or maximize some attributes of an executable computer program. Common requirements are to minimize a program's execution time, memory footprint, storage size, and power consumption (the last three being popular for portable computers).


2 Answers

Scott Hanselman has a blog post that shows a few examples of what /optimize (which is enabled in Release Builds) does.

As a summary: /optimize does many things with no exact number or definition given, but one of the more visible are method inlining (If you have a Method A() which calls B() which calls C() which calls D(), the compiler may "skip" B and C and go from A to D directly), which may cause a "weird" callstack in the Release build.

like image 66
Michael Stum Avatar answered Nov 20 '22 18:11

Michael Stum


It is disabled by default for debug builds. For Release builds it is enabled.

It is definitely worth enabling this switch as the compiler makes lots of tweaks and optimizations depending on the kind of code you have. For eg: Skipping redundant initializations, comparisons that never change etc.

Note: You might have some difficulty debugging if your turn on optimization as the code you have and the IL code that is generated may not match. This is the reason it is turned on only for Release builds.

like image 22
bobbyalex Avatar answered Nov 20 '22 17:11

bobbyalex