Optimal Code Compiling in C: Nitika Gupta Nistha Seth Prabhat Verma
Optimal Code Compiling in C: Nitika Gupta Nistha Seth Prabhat Verma
Abstract- In the present era, developing high level application includes finding a bottleneck, a critical part of the code
programs has been a major concern for the programmers. which is the primary consumer of the needed resources [1].
During the design phase of software application, the The code optimization mainly concerns on correctness that
functionality of developed programs has always been the area means optimization does not change the correctness of
of consideration. Different compilers may have in built
mechanism to speed up the programs, but these compilation
generated code. The criterion of code optimization must
process may adversely affect the length of the code and hence preserve the semantic equivalence of the program and the
slower the execution of highly developed applications. Thus, to algorithm should not be modified. Transformation, on
enhance the performance of the large complex applications, average should speed up the execution of program. In
several code optimization techniques are being provided by compiler optimization theory, the compiler optimization
the compilers in C. These code expansion techniques are basically refers to the program optimization to achieve
predominated over manual coding techniques as they help in performance in the execution. Program optimization refers
speeding up the process of program execution in such a way to the three aspects:-
that both time and reserved memory space can be effectively i. Frontend: a programming language code.
utilized. In this paper we discuss about our implemented work
on Code Optimization using two techniques: “Dead Code
ii. Intermediate code: an assembly language code
Elimination” and “Inlining”. Our implemented work is generated by the compiler appropriate to the
automatic procedure which eliminates the chances of errors programming language.
that are quite possible in manual procedures. We have verified iii. Backend: the specific machine or object code
the code optimization performance using code complexity generated from the assembly language code for the
measurement tools. Results obtained are quite satisfactory as actual execution by the compiler.
compare to existing methods.
II. EXISTING APPROACHES OF OPTIMIZATION
Index terms- Code Optimization techniques, complexity, A. Need for Optimization?
compilers, CCCC tool, functions, software.
Since process of optimization takes extra time than the
actual time involved in coding, our area of focus should be
I. INTRODUCTION
more on that 10% executed time- critical program rather
In compiler design, Optimization is the process of
than considering of implementation of whole program
transforming a piece of code (un-optimized code) to make
These fragments of code created a congestion in the process
more efficient without changing its output or side effects. A
of implementation and hence can be detected by special
program may be optimized so that it becomes of a smaller
utilities profilers which can measure the execution time of
size, consumes less memory, executes more rapidly, or
various parts of the program. Such optimization methods
performs fewer input/output operations. Optimization can
are done only during the stage of “complex programming”
be performed by automatic optimizers or programmers. An
and therefore is a mixture of several optimization
optimizer is either a specialized software tool or a built-in
approaches such as, refactoring and debugging:
unit of a compiler. Optimization is classified into high-level
simplification of "queer" constructs like strlen(path.c_str()),
and low-level optimization. High-level optimization are
logical conditions like (a.x != 0 && a.x != 0), and so on.
generally performed by the programmer who handles
Profilers are of little help with this kind of optimization and
abstract entities (functions, procedures, classes, etc.) and
hence such issues can be resolved by the usage of statistics
performed at the level of elementary structural blocks of
analytics tools. Such inefficient code may result due to
source code- loops, branches, etc. Low-level optimizations
programming errors and hence code fragmentation is done
are performed at the stage when source code is compiled
to detect every part of program. These tools analyze each
into a set of machine instructions, and it is at this stage that
fragment of the code and hence generate the warning
automated optimization is usually employed. Optimization
messages.
Source IR IR Machine
code FRONTEND code
OPTIMIZER BACKEND
Figure 1.Code Optimization
www.ijcsit.com 2050
Nitika Gupta/ (IJCSIT) International Journal of Computer Science and Information Technologies, Vol. 6 (3) , 2015, 2050-2057
B. Which code to optimize? done keeping in mind the time involved and influence of
Code optimization done manually creates problem: one code on the system performance [9]. The better approach is
doesn't only need to know how exactly optimization should to perform designing before coding leading to code
be done, but also what particular part of the program should fragmentation and hence avoid the unexpected
be optimized. Due to various reasons (slow input programming problems. Such form of developed code after
operations, the difference in the working speed of a human design phase is being clear and precise, thus
operator and a computer, and so on), 90% of the execution outperforming the cost involved in maintaining it.
time of a program is spent executing only 10% of the code. C. Merits of Optimization
Since optimization takes more time developing the Code optimization is a set of methods of code
program, one should better focus on optimizing these time- modification to improve code quality and efficiency.
critical 10% of code rather than try to optimize the whole A program may be optimized so that it becomes of a
program[11]. These code fragments are known as smaller size.
bottlenecks and can be detected by special utilities - A program may be optimized so that it consumes less
profilers - which can measure the time taken by various memory.
parts of the program to execute. An optimized code executes more rapidly.
The process of optimization can reduce readability and Optimization should increase the speed of the program
include more of coding thus enhancing the strength of and if possible, the program should demand less
program. However, in case of complex programs such number of resources.
process of optimization is difficult to achieve leading to Performs fewer input/output operations.
slower debugging. Thus optimization is done at the end of
Optimization gives high quality code with best
process of development of application. Initial optimization
complexity (time and space) without affecting the
of code lead to increase in the level of programming exact result of the code.
making it more complicated which interrupt the
.
programmer. The process of code expansion should be
INLINING contents of a function are “inlined”, basically, copied and pasted int add (int x, int y) int sub (int x, int y)
instead of a traditional call to that function[5] { return x+y; {
avoids the overhead of function calls. } return x-y;
int sub(int x,int y) }
{
return add(x , -y);
{
CODE identifying bits of code that occur within loops, but need only be void f(int a, int b) void f (int a, int b)
MOTION executed once during that particular loop.[5] { int i; { int i;
expensive re-evaluation will be potentially avoided for (i=1; i<10; i++) int tmp =a+b;
{ ar[i]=a+b; for (i=1; i<10; i++)
} { ar[i]=tmp; }
} }
COMMON common function: two operations produce same results[2] i = x + y + 1; t1 = x + y
SUB- Recomputing the expression can be eliminated j = x + y; i = t1 + 1;
EXPRESSION j = t1;
STRENGTH Computationally expensive operations by simpler ones. t := b * c t := b * c
REDUCTION Application: simplify multiplication by index variables to FOR i := 1 to 10000 DO d := 0
additions within loops[13] BEGIN FOR i := 1 TO 10000 DO
BEGIN
a := t d := i * 3
a := t
... d := d + 3
END …
END
LOOP Loop unwinding int i = 0; int i = 0;
UNROLLING Interpreting the iterations into a sequence of instructions which while (i < num) { while (i < num) {
will reduce the loop overhead. a_certain_function(i); a_certain_function(i);
i++; a_certain_function(i+1);
} a_certain_function(i+2);
a_certain_function(i+3);
i += 4;
}
CONSTANT Evaluate constant expressions at compile time. C:=1+3 C:=4
FOLDING Only possible when side-effect freeness guaranteed. True not False
www.ijcsit.com 2051
Nitika Gupta/ (IJCSIT) International Journal of Computer Science and Information Technologies, Vol. 6 (3) , 2015, 2050-2057
D. COMMON OPTIMIZATION TECHNIQUES All output files are now generated into a single
Optimization techniques are used to improve the directory.
speed of computer program. It focuses on minimizing time
spent by the CPU and gives sample source code 2. Counting Methods
transformations that often yield improvements. Table CCCC calculate each of the measures by implementing
1gives a brief overview of these optimization techniques simple algorithm. These algorithms are intended to give a
useful approximation to the underlying quantities.
E. CCCC TOOL a. Number of Modules (NOM):
CCCC is a tool for the analysis of source code in CCCC defines modules in terms of grouping of
various languages (primarily C++), which generates a member functions : C++ classes and namespace, java
report in HTML format on various measurements of the classes and interfaces and Ada packages are all defined
code processed. Although the tool was originally as modules.
implemented to process C++ and ANSI C, the present b. Line of Code (LOC):
version is also able to process Java source files, and support It includes industry standard of counting non-blank,
has been presented in earlier versions for Ada95. The name non-comment lines of source code. Class and Function
CCCC stands for 'C and C++ CodeCounter'. Measurements declaration are counted, but declarations of global data
of source code of this kind are generally referred to as are ignored.
'software metrics', or more precisely 'software product c. Comment Lines (COM):
metrics'. CCCC has been developed as freeware, and is Any line which contains any part of a comment for
released in source code form. Users are encouraged to the language concerned is treated as a comment by
compile the program themselves, and to modify the source CCCC. The leading comments are treated as part of the
to reflect their preferences and interests. The simplest way function or class definition which follows them.
of using CCCC is just to run it with the names of a selection d. Cabe’s cyclomatic complexity (MVG):
of files on the command line like this: It is the count of linearly independent paths through a
cccc my_types.h big.h small.h *.cc flow of control derived from a subprogram. In case of
For each file, named, CCCC will examine the extension of C++, the count is incremented for each of the
the filename, and if the extension is recognized as following tokens: ‘if’, ‘while’, ‘for’, ‘switch’, ‘break’,
indicating a supported language, the appropriate parser will ‘&&’, ‘||’.
run on the file. As each file is parsed, recognition of certain e. Weighted methods per class (WMC):
constructs will cause records to be written into an internal This is a count of the member functions known to exist
database. When all files have been processed, a report on in a class. Knowledge of existence of a function is only
the contents of the internal database will be generated in gained from declarations or definitions directly
HTML format. By default the main summary HTML report contained in files processed by CCCC.
is generated to the file cccc.htm in a subdirectory called
.cccc of the current working directory, with detailed reports When applying these existing approaches we have to face
on each module (i.e. C++ or Java class) identified by the some problems. Sometimes, it changes the meaning of the
analysis run. In addition to the summary and detailed code and final output. So that we built an interface in which
HTML reports, the run will cause generation of we embed optimization techniques in order to optimize the
corresponding summary and detailed reports in XML code. This is an automatic process in which time, space and
format, and a further file called cccc.db to be created. [7] cost is reduced in comparison to manual code optimization,
The report contains a number of tables identifying the will give quiet satisfactory result and easy to optimize with
modules in the files submitted and covering: complexity measures.
www.ijcsit.com 2052
Nitika Gupta/ (IJCSIT) International Journal of Computer Science and Information Technologies, Vol. 6 (3) , 2015, 2050-2057
techniques in order to optimize the code. It imports a code figure 3). After removing the dead code, in the bin folder
from your computer and then some operations are an optimized code file will be automatically generated with
performed according to the techniques implemented and the name dump.cpp. In Second phase, Program check is
then it calls the CCCC tool automatically to find the used to analyse the performance of optimized code by using
complexity of an optimized code. the CCCC Tool which was already linked with code
Our design works on two modes- analysis link(as shown in figure 4) and after that it will give
i. First mode: - Program is typed already in the program the resultant table of complexity measures(shown in figure
file and we will let the user open the existing file. 5).
ii. Second mode: - User will type the program in the space
provided.
A. DEAD CODE ELIMINATION
Dead Code is code that is either never executed or, if it
is executed, its result is never used by the program.
Unreachable code can be eliminated. Code that follows a
return, break, continue, goto and has no label, can be
eliminated.[2]
int f(int x){ int f(int x){
Figure 3. Code Analysis
Figure 2. Example Dead code elimination
int app ;
void f()
{ int i;
i=1;
/*dead store*/
app=1; Figure 4. Complexity Check
/*dead store*/
app=2;
return ;
app=3;
/*unreachable*/
}
int app;
void f( )
{ app=2;
return;
}
www.ijcsit.com 2053
Nitika Gupta/ (IJCSIT) International Journal of Computer Science and Information Technologies, Vol. 6 (3) , 2015, 2050-2057
B. INLINING
Inlining refers to compile-time optimization where a
small function of code will be injected into the calling
function rather than require a separate call. It solves the
performance and maintainability issue by letting you
declare the function as inline (at least in C++), [3]so that
when you call that function - instead of having your app
jumping around at runtime - the code in the inline function
is injected at compile time every time that given function is
called. There is an example of inlining i.e.
void swap(int & m, int & n)
Figure 6. Complexity Measurement Report Before { int temp = m;
Optimization m = n;
n = temp;
}
www.ijcsit.com 2054
Nitika Gupta/ (IJCSIT) International Journal of Computer Science and Information Technologies, Vol. 6 (3) , 2015, 2050-2057
After Inlining -
int temp = x;
x = y;
y = temp;
www.ijcsit.com 2055
Nitika Gupta/ (IJCSIT) International Journal of Computer Science and Information Technologies, Vol. 6 (3)After
, 2015,optimization
2050-2057
Before Optimization
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
/* function prototype */
float average(float , float,float); #define SQUARE(NUM) NUM*NUM
void main() #define SUM(a,b) (a+b)
{ float a,b,c,avg;
/* function prototype */
clrscr(); float average(float , float, float);
printf(“\nEnter the value of a,b,c :”); void main()
scanf(“%f %f %f”,&a,&b,&c); { float a, b ,c, avg;
avg=average(a,b,c); /*function call */ clrscr();
printf(“\nAverage = %f”,avg); printf(“\nEnter the value of a,b,c :”);
getch(); scanf(“%f %f %f”,&a,&b,&c);
} avg=average(a,b,c)
/* function definition */ /*function call */
float average(float i,float j,float k) printf(“\nAverage = %f”,avg);
{ float avg; getch();
avg=(i+j+k)/3; }
return avg;
/* function definition */
} float average(float I,float j,float k)
int sum(int a,int b) { float avg;
{ int c; avg=(i+j+k)/3;
c=a+b; return avg;
return c; }
} int sum(int a,int b)
int power(int x,int y) { int c;
{ int i,result; c=a+b;
result=1; return c;
for(i=1;i<=y;i++) }
result=result*x; int power(int x,int y)
return result; { int I,result;
} result=1;
int factorial(int num) for(i=1;i<=y;i++)
{ int fact=1,i; result=result*x;
for(i=1;i<=num;i++) return result;
fact=fact*i; }
return fact; int factorial(int num)
} { int fact=1,I;
int square(int num) for(i=1;i<=num;i++)
{ int result; fact=fact*I;
result=num*num; return fact;
return result; }
}
int operate(int a, int b)
{ return (a+b);
}
www.ijcsit.com 2056
Nitika Gupta/ (IJCSIT) International Journal of Computer Science and Information Technologies, Vol. 6 (3) , 2015, 2050-2057
REFERENCES
Table III. PERFORMANCE MEASUREMENT FOR [1] Michael E. Lee, “Optimization of Computer Programs in C”, Ontek
INLINING Corporation, USA “Code Optimization” article. Available:
http://leto.net/docs/C-optimization.php Forman, G. 2003.
[2] Maggie Johnson, “Code Optimization”, Handout 20, August 04,
Lines of Code (LOC) 47 38 2008.
McCabe’s Cyclomatic [3] Mohammed Fadle Abdulla, “Manual and Fast C Code Optimization”,
8 6 Anale. Seria Informatica. Vol. VIII fasc. I-2010.
Number [4] Mr. Chirag H. Bhatt, Dr. Harshad B. Bhadka, “Peephole
Lines of Comment 3 3 Optimization Technique for analysis and review of Compile Design
and Construction”, IOSR Journal of Computer Engineering (IOSR-
LOC/COM 15.667 12.667 JCE), Volume 9, Issue (4 Mar. - Apr. 2013).
[5] “Optimization Techniques in C”, Fall, 2013. Available:
MVG/COM 2.667 2.000 http://cs.brown.edu/courses/cs033/docs/guides/c_optimization_notes.
pdf.
[6] C. Brandolese, W. Fornaciari, F. Salice, D. Sciuto, “Source–Level
IV. RESULTS AND DISCUSSION Execution Time Estimation of C Programs”, Proceedings of the ninth
In this paper, we have proposed our approaches for code international symposium on Hardware/software code design.
optimization using techniques like Dead Code Elimination [7] “CCCC User Guide” available at
http://www.stderr.org/doc/cccc/CCCC%20User%20Guide.html
and Inlining for programming code written in C / C++ [8] Tips for “Optimizing C/C++ Code”. Available:
language. In Dead Code Elimination we are using cccc tool http://people.cs.clemson.edu/~dhouse/courses/405/papers/optimize.p
which was already linked with function and it will give df
result of complexities when the dead code found and give [9] “Writing Efficient C and C Code Optimization”. Article:
the optimized code with complexity measures. While using http://www.codeproject.com/Articles/6154/Writing-Efficient-C-and-
C-Code-Optimization
Inlining technique, we have used SQL server for making a [10] “Optimizing C++/ Code Optimization/ Faster operations”Available:
database of code function, and when we click on “optimize http://en.wikibooks.org/wiki/Optimizing_C%2B%2B/Code_optimiza
the code” after writing an un-optimized code, it will go to tion/Faster_operations.
database and match the code function, if it matched, the [11] “Continuous Compilation: A New Approach to Aggressive and
database gives the macro function as per the code function Adaptive Code Transformation”.
Available:http://ieeexplore.ieee.org/xpl/login.jsp?tp=&arnumber=12
requirement and optimize the code with minimum line of 13375&url=https%3A%2F%2Fsiteproxy.ruqli.workers.dev%3A443%2Fhttp%2Fieeexplore.ieee.org%2Fxpls%2Fabs_a
codes. We have verified the code optimization performance ll.jsp%3Farnumber%3D1213375\
using code complexity measurement tools. The results [12] S.K.Srivastava, Deepali Srivastava, “C in Depth” 3rd edition.
confirm a significant enhancement in quality of optimized [13] Programming language translation
http://web.cs.wpi.edu/~cs544/PLT10.2.3.html
code as computed using performance measuring tools.
[14] www.sm.luth.se_csee_courses_smd_163_lecture11.pdf Viktor Leijon
& Peter Jonsson with slides by Johan Nordlander Contains material
generously provided by Mark P. Jones
www.ijcsit.com 2057