SlideShare a Scribd company logo
code optimization techniques
● A substantial portion of a knowledge
worker's life may be spent waiting for a
computer program to produce output.
● Users and organizations control their wait time
by purchasing faster computers, adding memory, or using
faster network connections.
● Developers of application programs have a responsibility to
design their programs make the best use of these limited and
expensive resources.
What Is Code Optimization
● Optimization is the process of transforming a piece of code to make
it more efficient without changing its output
● Code optimization is a set of methods for code modification that
improves the code quality and efficiency.
● Optimization aims at -smaller size
-less memory consumption
-rapid executions (only few I/O operations)
Where To Optimize
● Check which portion or which module of the program is
running slow or using huge memory.
● If each part is separately being optimized then the total
programme will automatically get optimised.
● Optimization should be done on those part of program that
are run the most,especially on those methods which are called
repeatedly
Code Optimization Techniques
A program consist of :
• variables
• control statements
decision
loops
• functions
we need to optimize all these parts to run the program faster
Variable
The C compilers support the basic types
char,
short,
int,
long (signed and unsigned),
float and
double.
Using the most appropriate type for variables is very important, as it can reduce code
and data size and increase performance considerably.
Integers
Use unsigned int instead of int if we know the value will never be negative.
Some processors can handle unsigned integer arithmetic considerably faster than signed
So, the best declaration for an int variable in a tight loop would be:
unsigned int variable_name;
integer arithmetic is much faster than floating-point arithmetic, as it can usually be
done directly by the processor, rather than relying on external FPUs or floating point
math libraries.
Global variables
● Global variables are never allocated to registers.
● Global variables can be changed by assigning them indirectly using a pointer, or
by a function call.
● The compiler cannot cache the value of a global variable in a register, resulting in
extra loads and stores when globals are used.
int f(void);
int g(void);
int errs;
void test1(void)
{
errs += f();
errs += g();
}
Here a function uses global
variables heavily
Global variables
int f(void);
int g(void);
int errs;
void test1(void)
{
errs += f();
errs += g();
}
it is beneficial to copy
those global variables into
local variables so that they
can be assigned to
registers.
If a function uses global
variables heavily
void test2(void)
{
unsigned int localerrs = errs;
localerrs += f();
localerrs += g();
errs = localerrs;
}
Try it out….
Write a c program to find sum of N integer
#include<stdio.h>
#include<conio.h>
main()
{
int n=10,sum=0,i;
for(i=0;i<n;i++)
sum=sum+i;
printf("Sum:%d",sum);
getch();
}
Try it out….
write a c program to find sum of N integer
#include<stdio.h>
#include<conio.h>
main()
{
int n=10,sum=0,i;
for(i=0;i<n;i++)
sum=sum+i;
printf("Sum:%d",sum);
getch();
}
#include<stdio.h>
#include<conio.h>
main()
{
unsigned int n=10,sum=0,i;
for(i=0;i<n;i++)
sum=sum+i;
printf("Sum:%d",sum);
getch();
}
#include <stdio.h>
int area=100;
int square(){
int x=10;
return x*x; }
int rectangle(){
int l=10,b=20;
int ar=2*(l+b);
return ar;
}
main()
{
area=area+square();
area=area+rectangle();
printf("area:%d",area);
}
Try it out
Write a program to find total area of a plot having a square shaped and rectangular shaped plot
global variable
#include <stdio.h>
int area=100;
int square(){
int x=10;
return x*x; }
int rectangle(){
int l=10,b=20;
int ar=2*(l+b);
return ar;
}
Try it out
Write a program to find total area of a plot having a square shaped and rectangular shaped plot
Making global variable local
main()
{
unsigned int localarea=area;
area=area+square();
area=area+rectangle();
printf("area:%d",area);
area=localarea;
}
Using array indices
switch ( queue ) {
case 0 : letter = 'W';
break;
case 1 : letter = 'S';
break;
case 2 : letter = 'U';
break;
}
if ( queue == 0 )
letter = 'W';
else if ( queue == 1
)
letter = 'S';
else
letter = 'U';
If you wished to set a variable to a particular character, depending upon the value of
something, you might do this:
Using array indices
A neater (and quicker) method is to simply use the value as an
index into a character array, e.g.:
static char *classes="WSU";
letter = classes[queue];
Control Statements
switch() instead of if...else...
For large decisions involving if...else...else..., like this:
if( val == 1)
dostuff1();
else if (val == 2)
dostuff2();
else if (val == 3)
dostuff3();
Control Statements
switch() instead of if...else...
It may be faster to use a switch
switch( val )
{
case 1: dostuff1(); break;
case 2: dostuff2(); break;
case 3: dostuff3(); break;
}
Try it out...
Write a c program to set index to english alphabet
#include<stdio.h>
#include<string.h>
main()
{char letter; int ch;
printf("enter the pos");
scanf("%d",&ch);
switch(ch){
case 0:letter='a';break;
case 1:letter='b'; break;
- - -
}
printf("%c",letter); }
Try it out...
write a c program to set index to english alphabet
#include<stdio.h>
#include<string.h>
main()
{char letter; int ch;
printf("enter the pos");
scanf("%d",&ch);
switch(ch){
case 0:letter='a';break;
case 1:letter='b'; break;
- - -
}
printf("%c",letter); }
#include<stdio.h>
#include<conio.h>
main()
{
char *classes="abcdefghi";
int i,ch,letter[26],queue=0;;
for(i=1;i<=26;i++) {
letter[i]=classes[queue];
queue++;
}
printf("enter the position");
scanf("%d",&ch);
printf("%c",letter[ch]);
getch(); }
Loop jamming
Never use two loops where one will suffice
for(i=0; i<100; i++){
stuff();
}
for(i=0; i<100; i++){
morestuff();
}
Loop jamming
It will be better to do this way:
for(i=0; i<100; i++){
stuff();
}
for(i=0; i<100; i++){
morestuff();
}
for(i=0; i<100; ++)
{
stuff();
morestuff();
}
Try it out….
main()
{int a[100],sum=0,product=1,i;
printf("enter elements into array");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
for(i=0;i<5;i++)
sum=sum+a[i];
for(i=0;i<5;i++)
product=product*a[i];
printf("nsum:%d n
product:%d",sum,product); }
Write program to find sum and product of elements in a array
Try it out….
main()
{int a[100],sum=0,product=1,i;
printf("enter elements into array");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
for(i=0;i<5;i++)
sum=sum+a[i];
for(i=0;i<5;i++)
product=product*a[i];
printf("nsum:%d n
product:%d",sum,product); }
Write program to find sum and product of elements in a array
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
sum=sum+a[i];
product=product*a[i];
}
Function Looping
If a function is often called from within a loop ,it may be possible to put that loop
inside the function to cut down the overhead of calling the function repeatedly
for(i=0 ; i<100 ; i++)
{
func(t,i);
}
-
-
-
void func(int w,d)
{
lots of stuff.
}
func(t);
-
-
void func(w)
{
for(i=0 ; i<100 ; i++)
{
//lots of stuff.
}
}
Try it out
Write a program to find sum of n numbers
#include<stdio.h>
#include<conio.h>
main()
{
int n=10,i,total=0;
for(i=0;i<10;i++)
total+=sum(i);
printf("n sum is:%d",total);
getch();
}
sum(int i)
{ int sum=0;
sum=sum+i;
}
Try it out
Write a program to find sum of n numbers
#include<stdio.h>
#include<conio.h>
main()
{
int n=10,i,total=0;
for(i=0;i<10;i++)
total += sum(i);
printf("n sum is:%d",total);
getch();
}
sum(int i)
{ int sum=0;
sum=sum+i;
}
main()
{ int total=0;
total=sum();
printf("n sum is:%d",total);
getch();
}
int sum()
{ int sum=0,i;
for(i=0;i<10;i++)
sum=sum+i;
return sum;
}
Faster for() loops
Ordinarily, we used to code a simple for() loop like this:
for( i=0; i<10; i++){ ... } [ i loops through the values 0,1,2,3,4,5,6,7,8,9 ]
If we needn't care about the order of the loop counter, we can do this instead:
for( i=10; ; i-- ) { ... }
Using this code, i loops through the values 9,8,7,6,5,4,3,2,1,0, and the loop should be
faster.
This works because it is quicker to process i--
Try it out….
Write a program to find factorial of a number
main()
{
int factorial=0,n=3;
factorial=fact(n);
clrscr();
printf("%d",factorial);
getch();
}
fact(int n)
{
int i,fact=1;
for(i=0;i<n;i++)
fact*=i;
return fact;
}
Try it out….
Write a program to find factorial of a number
main()
{
int factorial=0,n=3;
factorial=fact(n);
clrscr();
printf("%d",factorial);
getch();
}
fact(int n)
{
int i,fact=1;
for(i=0;i<n;i++)
fact*=i;
return fact;
}
main()
{ int
factorial=0,n=3;
factorial=fact(n);
clrscr();
printf("%d",factorial);
getch();
}
fact(int n)
{ int i,fact=1;
for(i=n;i!=0;i--)
fact*=i;
return fact*;
}
loop unrolling
● Small loops can be unrolled for higher performance.
● If the loop iterates only a few times, it can be fully unrolled, so that the loop
overhead completely disappears.
This can make a big difference. It is well known that unrolling loops can produce
considerable savings, e.g.:
for(i=0; i<3; i++){
something(i);
}
something(0);
something(1);
something(2);
Early loop breaking
It is often not necessary to process the entirety of a loop.
Example, if we are searching an array for a particular item, break out of the loop as
soon as we have got what we need.
Example: this loop searches a list of 10000 numbers to see if there is a -99 in it.
found = FALSE;
for(i=0;i<10000;i++)
{
if( list[i] == -99 )
{
found = TRUE;
}
}
if( found ) printf("Yes,
there is a -99. Hooray!n");
found = FALSE;
for(i=0; i<10000; i++)
{
if( list[i] == -99 )
{
found = TRUE;
break;
}
}
if( found )
printf("Yes, there is a -
99. Hooray!n");
Try it out
write a program to find position of an element in array
main( )
{ int arr[10],i,element,f=0;
printf("enter the elements");
for(i=0;i<10;i++)
scanf("%d",&arr[i]);
printf("enter the elements to search");
scanf("%d",&element);
for(i=0;i<10;i++)
{ if(arr[i]==element)
f=1;
}
if(f = =1) printf("element found");
}
Try it out
write a program to find position of an element in array
main( )
{ int arr[10],i,element,f=0;
printf("enter the elements");
for(i=0;i<10;i++)
scanf("%d",&arr[i]);
printf("enter the elements to search");
scanf("%d",&element);
for(i=0;i<10;i++)
{ if(arr[i]==element)
f=1;
}
if(f = =1) printf("element found");
}
early loop
breaking
main( ) {
int arr[10],i,element,f=0;
printf("enter the elements");
for(i=0;i<10;i++)
scanf("%d",&arr[i]);
printf("enter the elements to search");
scanf("%d",&element);
for(i=0;i<10;i++)
{ if(arr[i]==element)
{ f=1; break;}
}
if(f = =1) printf("element found");
}
Code Motion
Code motion involves identifying bits of code that occur within loops, but need only
be executed once during that particular loop.
For example,
void shift_char(char *str){
int i;
for(i=0;i<strlen(str);i++){
str[i]++;
}
}
void shift_char(char *str){
int i;
int len=strlen(str)
for(i=0;i<len;i++){
str[i]++;
}
}
Try it out
Write a program to search a character in a string
main()
{ char arr[10];
int i,ch,f=0,len;
printf("enter the string");
scanf("%s",&arr);
printf("enter the character to
search");
scanf("%s",&ch);
len=strlen(arr);
for(i=0;i<len;i++)
{ if(arr[i]==ch)
{ f=1; break;}
}
if(f==1) printf("element found");
}
function strlen is called once and set
to variable len
Function Design
● keep functions small and simple.
● This enables the compiler to perform optimizations
such as register allocation more efficiently.
function call overhead
● There is some limitation upto which words of argument
can be passed to a function in register.
● If the argument limitation is 4 then the 5th and
subsequent words are passed on the stack.
int f2(int a, int b, int c, int d, int e, int f)
{
return a + b + c + d + e + f;
}
int g2(void) {
return f2(1, 2, 3, 4, 5, 6);
}
The fifth and sixth parameters are stored on the stack in g2, and reloaded
in f2, costing two memory accesses per parameter.
int f1(int a, int b, int c, int d)
{
return a + b + c + d;
}
int g1(void) {
return f1(1, 2, 3, 4);
}
This code works fine.
Minimizing parameter passing overhead
To minimize the overhead of passing parameters to functions:
● Try to ensure that small functions take four or fewer arguments.This will reduce
the number of parameters and increase readability.
● Pass pointers to structures instead of passing the structure itself.
void print_data_of_a_structure ( const Thestruct *data_pointer)
{
...printf contents of the structure...
}
● Avoid functions with a variable number of parameters. Those functions effectively
pass all their arguments on the stack.
Try it out
write a c program to print content of a structure
struct student
{ int id;
char name[20];
float percentage };
void func(struct student *record);
int main()
{ struct student record;
record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;
func(&record);
return 0; }
void func(struct student *record)
{
printf(" Id is: %d n", record->id);
printf(" Name is: %s n", record->name);
printf(" Percentage is: %f n", record->percentage);
}
struct student
{ int id;
char name[20];
float percentage; };
void func(struct student record);
int main()
{ struct student record;
record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;
func(record);
return 0; }
void func(struct student record)
{ printf(" Id is: %d n", record.id);
printf(" Name is: %s n", record.name);
printf(" Percentage is: %f n",
record.percentage);
}
Pass pointers of a
structure
Inline function
● Inlining is the process by which the contents of a function are copied and pasted
instead of a traditional call to that function.
● This form of optimization avoids the overhead of function calls, by eliminating the
need to jump, create a new stack frame, and reverse this process at the end of the
function.
Inline int square(int x)
{
return x*x
}
#include<math.h>
double length(int x,int y)
{
return sqrt(square(x)*square(y))
}
Try it out
write a program to implement square root of x2+y2
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int x=4,y=3,a;
a= sqrt(square(x)+square(y));
clrscr();
printf("n %d",a);
getch();
}
int square(int x)
{ return x*x;
}
#include <stdio.h>
#include<math.h>
int main() {
int tmp,x=4,y=3;
tmp = sqrt(square(x)+square(y));
printf("square val=%dn", tmp);
return 0;
}
int inline square(int x) {
return x*x;
}
Inline function
Advantage of using inline function
● No function call overhead
As the code is substituted directly ,there is no overhead like saving and storing
register
● Lower argument evaluation overhead
The overhead of parameter passing is generally lower,since it is not
necessary to copy variables
THE END

More Related Content

What's hot

Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazation
Siva Sathya
 
Compiler Optimization Presentation
Compiler Optimization PresentationCompiler Optimization Presentation
Compiler Optimization Presentation
19magnet
 
Compiler optimization
Compiler optimizationCompiler optimization
Compiler optimization
ZongYing Lyu
 
openMP loop parallelization
openMP loop parallelizationopenMP loop parallelization
openMP loop parallelization
Albert DeFusco
 
Dataflow Analysis
Dataflow AnalysisDataflow Analysis
Dataflow Analysis
Eelco Visser
 
Three address code generation
Three address code generationThree address code generation
Three address code generation
Rabin BK
 
Principal source of optimization in compiler design
Principal source of optimization in compiler designPrincipal source of optimization in compiler design
Principal source of optimization in compiler design
Rajkumar R
 
Three Address code
Three Address code Three Address code
Three Address code
Pooja Dixit
 
Code Generation
Code GenerationCode Generation
Code Generation
PrabuPappuR
 
C++ Language
C++ LanguageC++ Language
C++ Language
Syed Zaid Irshad
 
Compiler Design- Machine Independent Optimizations
Compiler Design- Machine Independent OptimizationsCompiler Design- Machine Independent Optimizations
Compiler Design- Machine Independent Optimizations
Jyothishmathi Institute of Technology and Science Karimnagar
 
COMPILER DESIGN AND CONSTRUCTION
COMPILER DESIGN AND CONSTRUCTIONCOMPILER DESIGN AND CONSTRUCTION
COMPILER DESIGN AND CONSTRUCTION
Anil Pokhrel
 
Teaching F#
Teaching F#Teaching F#
Teaching F#
Tomas Petricek
 
Basic Block
Basic BlockBasic Block
Basic Block
Shiv1234567
 
Cse115 lecture08repetitionstructures part02
Cse115 lecture08repetitionstructures part02Cse115 lecture08repetitionstructures part02
Cse115 lecture08repetitionstructures part02
Md. Ashikur Rahman
 

What's hot (19)

Optimization
OptimizationOptimization
Optimization
 
Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazation
 
Compiler Optimization Presentation
Compiler Optimization PresentationCompiler Optimization Presentation
Compiler Optimization Presentation
 
Compiler optimization
Compiler optimizationCompiler optimization
Compiler optimization
 
openMP loop parallelization
openMP loop parallelizationopenMP loop parallelization
openMP loop parallelization
 
Ch9b
Ch9bCh9b
Ch9b
 
Dataflow Analysis
Dataflow AnalysisDataflow Analysis
Dataflow Analysis
 
Three address code generation
Three address code generationThree address code generation
Three address code generation
 
Principal source of optimization in compiler design
Principal source of optimization in compiler designPrincipal source of optimization in compiler design
Principal source of optimization in compiler design
 
Three Address code
Three Address code Three Address code
Three Address code
 
Code Generation
Code GenerationCode Generation
Code Generation
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
Functions
FunctionsFunctions
Functions
 
Compiler Design- Machine Independent Optimizations
Compiler Design- Machine Independent OptimizationsCompiler Design- Machine Independent Optimizations
Compiler Design- Machine Independent Optimizations
 
COMPILER DESIGN AND CONSTRUCTION
COMPILER DESIGN AND CONSTRUCTIONCOMPILER DESIGN AND CONSTRUCTION
COMPILER DESIGN AND CONSTRUCTION
 
Teaching F#
Teaching F#Teaching F#
Teaching F#
 
Basic Block
Basic BlockBasic Block
Basic Block
 
Cse115 lecture08repetitionstructures part02
Cse115 lecture08repetitionstructures part02Cse115 lecture08repetitionstructures part02
Cse115 lecture08repetitionstructures part02
 
Ch8b
Ch8bCh8b
Ch8b
 

Viewers also liked

Code Optimization
Code OptimizationCode Optimization
Code Optimizationguest9f8315
 
Case statements
Case statementsCase statements
Case statements
Kalaimathi Vijayakumar
 
Assignment statements
Assignment statementsAssignment statements
Assignment statements
Divya Devan
 
Declarations
DeclarationsDeclarations
Declarations
sangeetha r
 
Unit iv(simple code generator)
Unit iv(simple code generator)Unit iv(simple code generator)
Unit iv(simple code generator)
Kalaimathi Vijayakumar
 
Dsip and its biometrics appln
Dsip and its biometrics applnDsip and its biometrics appln
Dsip and its biometrics appln
Dr. Vinayak Bharadi
 
code optimization
code optimization code optimization
code optimization
Sanjeev Raaz
 
Image pre processing - local processing
Image pre processing - local processingImage pre processing - local processing
Image pre processing - local processingAshish Kumar
 
Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2
Iffat Anjum
 
Peephole optimization techniques in compiler design
Peephole optimization techniques in compiler designPeephole optimization techniques in compiler design
Peephole optimization techniques in compiler design
Anul Chaudhary
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
Shine Raj
 
Code generation
Code generationCode generation
Code generation
Aparna Nayak
 
Image pre processing
Image pre processingImage pre processing
Image pre processingAshish Kumar
 
Image Processing Basics
Image Processing BasicsImage Processing Basics
Image Processing Basics
Nam Le
 
Procedure calls(compilar)
Procedure  calls(compilar)Procedure  calls(compilar)
Procedure calls(compilar)
Kalaimathi Vijayakumar
 
Digital Image Processing
Digital Image ProcessingDigital Image Processing
Digital Image Processing
Sahil Biswas
 

Viewers also liked (17)

Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
Case statements
Case statementsCase statements
Case statements
 
Assignment statements
Assignment statementsAssignment statements
Assignment statements
 
Declarations
DeclarationsDeclarations
Declarations
 
ASSIGNMENT STATEMENTS AND
ASSIGNMENT STATEMENTS ANDASSIGNMENT STATEMENTS AND
ASSIGNMENT STATEMENTS AND
 
Unit iv(simple code generator)
Unit iv(simple code generator)Unit iv(simple code generator)
Unit iv(simple code generator)
 
Dsip and its biometrics appln
Dsip and its biometrics applnDsip and its biometrics appln
Dsip and its biometrics appln
 
code optimization
code optimization code optimization
code optimization
 
Image pre processing - local processing
Image pre processing - local processingImage pre processing - local processing
Image pre processing - local processing
 
Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2
 
Peephole optimization techniques in compiler design
Peephole optimization techniques in compiler designPeephole optimization techniques in compiler design
Peephole optimization techniques in compiler design
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
 
Code generation
Code generationCode generation
Code generation
 
Image pre processing
Image pre processingImage pre processing
Image pre processing
 
Image Processing Basics
Image Processing BasicsImage Processing Basics
Image Processing Basics
 
Procedure calls(compilar)
Procedure  calls(compilar)Procedure  calls(compilar)
Procedure calls(compilar)
 
Digital Image Processing
Digital Image ProcessingDigital Image Processing
Digital Image Processing
 

Similar to Code optimization

An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of c
Tushar B Kute
 
Array Cont
Array ContArray Cont
Embedded C - Day 2
Embedded C - Day 2Embedded C - Day 2
COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops
Hemantha Kulathilake
 
C intro
C introC intro
C intro
Kamran
 
Fundamentals of computer programming by Dr. A. Charan Kumari
Fundamentals of computer programming by Dr. A. Charan KumariFundamentals of computer programming by Dr. A. Charan Kumari
Fundamentals of computer programming by Dr. A. Charan Kumari
THE NORTHCAP UNIVERSITY
 
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxPROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
amrit47
 
C important questions
C important questionsC important questions
C important questions
JYOTI RANJAN PAL
 
Learning C programming - from lynxbee.com
Learning C programming - from lynxbee.comLearning C programming - from lynxbee.com
Learning C programming - from lynxbee.com
Green Ecosystem
 
7 functions
7  functions7  functions
7 functions
MomenMostafa
 
Function in c program
Function in c programFunction in c program
Function in c program
umesh patil
 
Functions
FunctionsFunctions
Functions
Swarup Boro
 
Fundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptxFundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptx
Chandrakant Divate
 
Advanced C - Part 2
Advanced C - Part 2Advanced C - Part 2
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01
Wingston
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
MKalpanaDevi
 
C++ Function
C++ FunctionC++ Function
C++ FunctionHajar
 
Assignment c programming
Assignment c programmingAssignment c programming
Assignment c programming
Icaii Infotech
 
BKK16-503 Undefined Behavior and Compiler Optimizations – Why Your Program St...
BKK16-503 Undefined Behavior and Compiler Optimizations – Why Your Program St...BKK16-503 Undefined Behavior and Compiler Optimizations – Why Your Program St...
BKK16-503 Undefined Behavior and Compiler Optimizations – Why Your Program St...
Linaro
 

Similar to Code optimization (20)

An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of c
 
Array Cont
Array ContArray Cont
Array Cont
 
Embedded C - Day 2
Embedded C - Day 2Embedded C - Day 2
Embedded C - Day 2
 
COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops
 
C intro
C introC intro
C intro
 
Fundamentals of computer programming by Dr. A. Charan Kumari
Fundamentals of computer programming by Dr. A. Charan KumariFundamentals of computer programming by Dr. A. Charan Kumari
Fundamentals of computer programming by Dr. A. Charan Kumari
 
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxPROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
 
C important questions
C important questionsC important questions
C important questions
 
Learning C programming - from lynxbee.com
Learning C programming - from lynxbee.comLearning C programming - from lynxbee.com
Learning C programming - from lynxbee.com
 
7 functions
7  functions7  functions
7 functions
 
Function in c program
Function in c programFunction in c program
Function in c program
 
functions
functionsfunctions
functions
 
Functions
FunctionsFunctions
Functions
 
Fundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptxFundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptx
 
Advanced C - Part 2
Advanced C - Part 2Advanced C - Part 2
Advanced C - Part 2
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
Assignment c programming
Assignment c programmingAssignment c programming
Assignment c programming
 
BKK16-503 Undefined Behavior and Compiler Optimizations – Why Your Program St...
BKK16-503 Undefined Behavior and Compiler Optimizations – Why Your Program St...BKK16-503 Undefined Behavior and Compiler Optimizations – Why Your Program St...
BKK16-503 Undefined Behavior and Compiler Optimizations – Why Your Program St...
 

More from baabtra.com - No. 1 supplier of quality freshers

Acquiring new skills what you should know
Acquiring new skills   what you should knowAcquiring new skills   what you should know
Acquiring new skills what you should know
baabtra.com - No. 1 supplier of quality freshers
 
Baabtra.com programming at school
Baabtra.com programming at schoolBaabtra.com programming at school
Baabtra.com programming at school
baabtra.com - No. 1 supplier of quality freshers
 
99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love 99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love
baabtra.com - No. 1 supplier of quality freshers
 
Php database connectivity
Php database connectivityPhp database connectivity
Chapter 6 database normalisation
Chapter 6  database normalisationChapter 6  database normalisation
Chapter 6 database normalisation
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 5 transactions and dcl statements
Chapter 5  transactions and dcl statementsChapter 5  transactions and dcl statements
Chapter 5 transactions and dcl statements
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 4 functions, views, indexing
Chapter 4  functions, views, indexingChapter 4  functions, views, indexing
Chapter 4 functions, views, indexing
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 3 stored procedures
Chapter 3 stored proceduresChapter 3 stored procedures
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 2  grouping,scalar and aggergate functions,joins   inner join,outer joinChapter 2  grouping,scalar and aggergate functions,joins   inner join,outer join
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
 
Microsoft holo lens
Microsoft holo lensMicrosoft holo lens
Blue brain
Blue brainBlue brain
5g
5g5g
Aptitude skills baabtra
Aptitude skills baabtraAptitude skills baabtra

More from baabtra.com - No. 1 supplier of quality freshers (20)

Agile methodology and scrum development
Agile methodology and scrum developmentAgile methodology and scrum development
Agile methodology and scrum development
 
Best coding practices
Best coding practicesBest coding practices
Best coding practices
 
Core java - baabtra
Core java - baabtraCore java - baabtra
Core java - baabtra
 
Acquiring new skills what you should know
Acquiring new skills   what you should knowAcquiring new skills   what you should know
Acquiring new skills what you should know
 
Baabtra.com programming at school
Baabtra.com programming at schoolBaabtra.com programming at school
Baabtra.com programming at school
 
99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love 99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love
 
Php sessions & cookies
Php sessions & cookiesPhp sessions & cookies
Php sessions & cookies
 
Php database connectivity
Php database connectivityPhp database connectivity
Php database connectivity
 
Chapter 6 database normalisation
Chapter 6  database normalisationChapter 6  database normalisation
Chapter 6 database normalisation
 
Chapter 5 transactions and dcl statements
Chapter 5  transactions and dcl statementsChapter 5  transactions and dcl statements
Chapter 5 transactions and dcl statements
 
Chapter 4 functions, views, indexing
Chapter 4  functions, views, indexingChapter 4  functions, views, indexing
Chapter 4 functions, views, indexing
 
Chapter 3 stored procedures
Chapter 3 stored proceduresChapter 3 stored procedures
Chapter 3 stored procedures
 
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 2  grouping,scalar and aggergate functions,joins   inner join,outer joinChapter 2  grouping,scalar and aggergate functions,joins   inner join,outer join
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Microsoft holo lens
Microsoft holo lensMicrosoft holo lens
Microsoft holo lens
 
Blue brain
Blue brainBlue brain
Blue brain
 
5g
5g5g
5g
 
Aptitude skills baabtra
Aptitude skills baabtraAptitude skills baabtra
Aptitude skills baabtra
 
Gd baabtra
Gd baabtraGd baabtra
Gd baabtra
 

Recently uploaded

Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 

Recently uploaded (20)

Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 

Code optimization

  • 1.
  • 3. ● A substantial portion of a knowledge worker's life may be spent waiting for a computer program to produce output. ● Users and organizations control their wait time by purchasing faster computers, adding memory, or using faster network connections. ● Developers of application programs have a responsibility to design their programs make the best use of these limited and expensive resources.
  • 4. What Is Code Optimization ● Optimization is the process of transforming a piece of code to make it more efficient without changing its output ● Code optimization is a set of methods for code modification that improves the code quality and efficiency. ● Optimization aims at -smaller size -less memory consumption -rapid executions (only few I/O operations)
  • 5. Where To Optimize ● Check which portion or which module of the program is running slow or using huge memory. ● If each part is separately being optimized then the total programme will automatically get optimised. ● Optimization should be done on those part of program that are run the most,especially on those methods which are called repeatedly
  • 6. Code Optimization Techniques A program consist of : • variables • control statements decision loops • functions we need to optimize all these parts to run the program faster
  • 7. Variable The C compilers support the basic types char, short, int, long (signed and unsigned), float and double. Using the most appropriate type for variables is very important, as it can reduce code and data size and increase performance considerably.
  • 8. Integers Use unsigned int instead of int if we know the value will never be negative. Some processors can handle unsigned integer arithmetic considerably faster than signed So, the best declaration for an int variable in a tight loop would be: unsigned int variable_name; integer arithmetic is much faster than floating-point arithmetic, as it can usually be done directly by the processor, rather than relying on external FPUs or floating point math libraries.
  • 9. Global variables ● Global variables are never allocated to registers. ● Global variables can be changed by assigning them indirectly using a pointer, or by a function call. ● The compiler cannot cache the value of a global variable in a register, resulting in extra loads and stores when globals are used. int f(void); int g(void); int errs; void test1(void) { errs += f(); errs += g(); } Here a function uses global variables heavily
  • 10. Global variables int f(void); int g(void); int errs; void test1(void) { errs += f(); errs += g(); } it is beneficial to copy those global variables into local variables so that they can be assigned to registers. If a function uses global variables heavily void test2(void) { unsigned int localerrs = errs; localerrs += f(); localerrs += g(); errs = localerrs; }
  • 11. Try it out…. Write a c program to find sum of N integer #include<stdio.h> #include<conio.h> main() { int n=10,sum=0,i; for(i=0;i<n;i++) sum=sum+i; printf("Sum:%d",sum); getch(); }
  • 12. Try it out…. write a c program to find sum of N integer #include<stdio.h> #include<conio.h> main() { int n=10,sum=0,i; for(i=0;i<n;i++) sum=sum+i; printf("Sum:%d",sum); getch(); } #include<stdio.h> #include<conio.h> main() { unsigned int n=10,sum=0,i; for(i=0;i<n;i++) sum=sum+i; printf("Sum:%d",sum); getch(); }
  • 13. #include <stdio.h> int area=100; int square(){ int x=10; return x*x; } int rectangle(){ int l=10,b=20; int ar=2*(l+b); return ar; } main() { area=area+square(); area=area+rectangle(); printf("area:%d",area); } Try it out Write a program to find total area of a plot having a square shaped and rectangular shaped plot global variable
  • 14. #include <stdio.h> int area=100; int square(){ int x=10; return x*x; } int rectangle(){ int l=10,b=20; int ar=2*(l+b); return ar; } Try it out Write a program to find total area of a plot having a square shaped and rectangular shaped plot Making global variable local main() { unsigned int localarea=area; area=area+square(); area=area+rectangle(); printf("area:%d",area); area=localarea; }
  • 15. Using array indices switch ( queue ) { case 0 : letter = 'W'; break; case 1 : letter = 'S'; break; case 2 : letter = 'U'; break; } if ( queue == 0 ) letter = 'W'; else if ( queue == 1 ) letter = 'S'; else letter = 'U'; If you wished to set a variable to a particular character, depending upon the value of something, you might do this:
  • 16. Using array indices A neater (and quicker) method is to simply use the value as an index into a character array, e.g.: static char *classes="WSU"; letter = classes[queue];
  • 17. Control Statements switch() instead of if...else... For large decisions involving if...else...else..., like this: if( val == 1) dostuff1(); else if (val == 2) dostuff2(); else if (val == 3) dostuff3();
  • 18. Control Statements switch() instead of if...else... It may be faster to use a switch switch( val ) { case 1: dostuff1(); break; case 2: dostuff2(); break; case 3: dostuff3(); break; }
  • 19. Try it out... Write a c program to set index to english alphabet #include<stdio.h> #include<string.h> main() {char letter; int ch; printf("enter the pos"); scanf("%d",&ch); switch(ch){ case 0:letter='a';break; case 1:letter='b'; break; - - - } printf("%c",letter); }
  • 20. Try it out... write a c program to set index to english alphabet #include<stdio.h> #include<string.h> main() {char letter; int ch; printf("enter the pos"); scanf("%d",&ch); switch(ch){ case 0:letter='a';break; case 1:letter='b'; break; - - - } printf("%c",letter); } #include<stdio.h> #include<conio.h> main() { char *classes="abcdefghi"; int i,ch,letter[26],queue=0;; for(i=1;i<=26;i++) { letter[i]=classes[queue]; queue++; } printf("enter the position"); scanf("%d",&ch); printf("%c",letter[ch]); getch(); }
  • 21. Loop jamming Never use two loops where one will suffice for(i=0; i<100; i++){ stuff(); } for(i=0; i<100; i++){ morestuff(); }
  • 22. Loop jamming It will be better to do this way: for(i=0; i<100; i++){ stuff(); } for(i=0; i<100; i++){ morestuff(); } for(i=0; i<100; ++) { stuff(); morestuff(); }
  • 23. Try it out…. main() {int a[100],sum=0,product=1,i; printf("enter elements into array"); for(i=0;i<5;i++) scanf("%d",&a[i]); for(i=0;i<5;i++) sum=sum+a[i]; for(i=0;i<5;i++) product=product*a[i]; printf("nsum:%d n product:%d",sum,product); } Write program to find sum and product of elements in a array
  • 24. Try it out…. main() {int a[100],sum=0,product=1,i; printf("enter elements into array"); for(i=0;i<5;i++) scanf("%d",&a[i]); for(i=0;i<5;i++) sum=sum+a[i]; for(i=0;i<5;i++) product=product*a[i]; printf("nsum:%d n product:%d",sum,product); } Write program to find sum and product of elements in a array for(i=0;i<5;i++) { scanf("%d",&a[i]); sum=sum+a[i]; product=product*a[i]; }
  • 25. Function Looping If a function is often called from within a loop ,it may be possible to put that loop inside the function to cut down the overhead of calling the function repeatedly for(i=0 ; i<100 ; i++) { func(t,i); } - - - void func(int w,d) { lots of stuff. } func(t); - - void func(w) { for(i=0 ; i<100 ; i++) { //lots of stuff. } }
  • 26. Try it out Write a program to find sum of n numbers #include<stdio.h> #include<conio.h> main() { int n=10,i,total=0; for(i=0;i<10;i++) total+=sum(i); printf("n sum is:%d",total); getch(); } sum(int i) { int sum=0; sum=sum+i; }
  • 27. Try it out Write a program to find sum of n numbers #include<stdio.h> #include<conio.h> main() { int n=10,i,total=0; for(i=0;i<10;i++) total += sum(i); printf("n sum is:%d",total); getch(); } sum(int i) { int sum=0; sum=sum+i; } main() { int total=0; total=sum(); printf("n sum is:%d",total); getch(); } int sum() { int sum=0,i; for(i=0;i<10;i++) sum=sum+i; return sum; }
  • 28. Faster for() loops Ordinarily, we used to code a simple for() loop like this: for( i=0; i<10; i++){ ... } [ i loops through the values 0,1,2,3,4,5,6,7,8,9 ] If we needn't care about the order of the loop counter, we can do this instead: for( i=10; ; i-- ) { ... } Using this code, i loops through the values 9,8,7,6,5,4,3,2,1,0, and the loop should be faster. This works because it is quicker to process i--
  • 29. Try it out…. Write a program to find factorial of a number main() { int factorial=0,n=3; factorial=fact(n); clrscr(); printf("%d",factorial); getch(); } fact(int n) { int i,fact=1; for(i=0;i<n;i++) fact*=i; return fact; }
  • 30. Try it out…. Write a program to find factorial of a number main() { int factorial=0,n=3; factorial=fact(n); clrscr(); printf("%d",factorial); getch(); } fact(int n) { int i,fact=1; for(i=0;i<n;i++) fact*=i; return fact; } main() { int factorial=0,n=3; factorial=fact(n); clrscr(); printf("%d",factorial); getch(); } fact(int n) { int i,fact=1; for(i=n;i!=0;i--) fact*=i; return fact*; }
  • 31. loop unrolling ● Small loops can be unrolled for higher performance. ● If the loop iterates only a few times, it can be fully unrolled, so that the loop overhead completely disappears. This can make a big difference. It is well known that unrolling loops can produce considerable savings, e.g.: for(i=0; i<3; i++){ something(i); } something(0); something(1); something(2);
  • 32. Early loop breaking It is often not necessary to process the entirety of a loop. Example, if we are searching an array for a particular item, break out of the loop as soon as we have got what we need. Example: this loop searches a list of 10000 numbers to see if there is a -99 in it. found = FALSE; for(i=0;i<10000;i++) { if( list[i] == -99 ) { found = TRUE; } } if( found ) printf("Yes, there is a -99. Hooray!n"); found = FALSE; for(i=0; i<10000; i++) { if( list[i] == -99 ) { found = TRUE; break; } } if( found ) printf("Yes, there is a - 99. Hooray!n");
  • 33. Try it out write a program to find position of an element in array main( ) { int arr[10],i,element,f=0; printf("enter the elements"); for(i=0;i<10;i++) scanf("%d",&arr[i]); printf("enter the elements to search"); scanf("%d",&element); for(i=0;i<10;i++) { if(arr[i]==element) f=1; } if(f = =1) printf("element found"); }
  • 34. Try it out write a program to find position of an element in array main( ) { int arr[10],i,element,f=0; printf("enter the elements"); for(i=0;i<10;i++) scanf("%d",&arr[i]); printf("enter the elements to search"); scanf("%d",&element); for(i=0;i<10;i++) { if(arr[i]==element) f=1; } if(f = =1) printf("element found"); } early loop breaking main( ) { int arr[10],i,element,f=0; printf("enter the elements"); for(i=0;i<10;i++) scanf("%d",&arr[i]); printf("enter the elements to search"); scanf("%d",&element); for(i=0;i<10;i++) { if(arr[i]==element) { f=1; break;} } if(f = =1) printf("element found"); }
  • 35. Code Motion Code motion involves identifying bits of code that occur within loops, but need only be executed once during that particular loop. For example, void shift_char(char *str){ int i; for(i=0;i<strlen(str);i++){ str[i]++; } } void shift_char(char *str){ int i; int len=strlen(str) for(i=0;i<len;i++){ str[i]++; } }
  • 36. Try it out Write a program to search a character in a string main() { char arr[10]; int i,ch,f=0,len; printf("enter the string"); scanf("%s",&arr); printf("enter the character to search"); scanf("%s",&ch); len=strlen(arr); for(i=0;i<len;i++) { if(arr[i]==ch) { f=1; break;} } if(f==1) printf("element found"); } function strlen is called once and set to variable len
  • 37. Function Design ● keep functions small and simple. ● This enables the compiler to perform optimizations such as register allocation more efficiently. function call overhead ● There is some limitation upto which words of argument can be passed to a function in register. ● If the argument limitation is 4 then the 5th and subsequent words are passed on the stack.
  • 38. int f2(int a, int b, int c, int d, int e, int f) { return a + b + c + d + e + f; } int g2(void) { return f2(1, 2, 3, 4, 5, 6); } The fifth and sixth parameters are stored on the stack in g2, and reloaded in f2, costing two memory accesses per parameter. int f1(int a, int b, int c, int d) { return a + b + c + d; } int g1(void) { return f1(1, 2, 3, 4); } This code works fine.
  • 39. Minimizing parameter passing overhead To minimize the overhead of passing parameters to functions: ● Try to ensure that small functions take four or fewer arguments.This will reduce the number of parameters and increase readability. ● Pass pointers to structures instead of passing the structure itself. void print_data_of_a_structure ( const Thestruct *data_pointer) { ...printf contents of the structure... } ● Avoid functions with a variable number of parameters. Those functions effectively pass all their arguments on the stack.
  • 40. Try it out write a c program to print content of a structure struct student { int id; char name[20]; float percentage }; void func(struct student *record); int main() { struct student record; record.id=1; strcpy(record.name, "Raju"); record.percentage = 86.5; func(&record); return 0; } void func(struct student *record) { printf(" Id is: %d n", record->id); printf(" Name is: %s n", record->name); printf(" Percentage is: %f n", record->percentage); } struct student { int id; char name[20]; float percentage; }; void func(struct student record); int main() { struct student record; record.id=1; strcpy(record.name, "Raju"); record.percentage = 86.5; func(record); return 0; } void func(struct student record) { printf(" Id is: %d n", record.id); printf(" Name is: %s n", record.name); printf(" Percentage is: %f n", record.percentage); } Pass pointers of a structure
  • 41. Inline function ● Inlining is the process by which the contents of a function are copied and pasted instead of a traditional call to that function. ● This form of optimization avoids the overhead of function calls, by eliminating the need to jump, create a new stack frame, and reverse this process at the end of the function. Inline int square(int x) { return x*x } #include<math.h> double length(int x,int y) { return sqrt(square(x)*square(y)) }
  • 42. Try it out write a program to implement square root of x2+y2 #include<stdio.h> #include<conio.h> #include<math.h> main() { int x=4,y=3,a; a= sqrt(square(x)+square(y)); clrscr(); printf("n %d",a); getch(); } int square(int x) { return x*x; } #include <stdio.h> #include<math.h> int main() { int tmp,x=4,y=3; tmp = sqrt(square(x)+square(y)); printf("square val=%dn", tmp); return 0; } int inline square(int x) { return x*x; } Inline function
  • 43. Advantage of using inline function ● No function call overhead As the code is substituted directly ,there is no overhead like saving and storing register ● Lower argument evaluation overhead The overhead of parameter passing is generally lower,since it is not necessary to copy variables