Module 14 - part 3 - Performance Efficiency of a C Program
Module 14 - part 3 - Performance Efficiency of a C Program
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Using Macros
int cube(int a)
{ vs #define cube(a) a*a*a
return a*a*a;
}
factr(1)
factr(2)
factr(3)
fact() factr(4)
• Example usage:-
• gcc –finline-functions myprog.c
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
In-line functions
Example
#include <stdio.h>
inline int mult(int a, int b){
return (a*b);
}
int main(){
int c;
c = mult(2, 3);
printf("Multiplication: %d", c);
return 0;
}
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
gcc compiler optimization flags
Various compiler optimization flags available with gcc:
• -o1
• -o2
• -o3
• -ofast
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Choosing Better Algorithms
Wherever alternative efficient algorithms are available, we must
consider using them.
For example:
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Minimizing Stack Usage
• A common problem that arises during program execution is stack
overflow error
• Which means that sufficient space is not available in the stack
segment for storing variables of a function.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Minimizing Stack Usage
(contd.)
• Consider this example: • When func2() is done it returns directly to
int func1(){ func1()'s caller.
int a, b, c; a) reduces the maximum depth of the stack
if(some_condition) b) saves the execution of some return-from-
return func2(); subroutine code as it will get executed only
else once instead of twice
return 1; – or more, depending on how deeply the
} function results are passed along.
• If func1() and func2() are the same function
• Since func1()'s last (recursion) the compiler can do something
statement is to call func2() else:
and func1() has no need • the stack can be left in place and the
of its variables after that call can be replaced by a goto back to
point, so can be removed the top of the function. This is called
from its stack frame. tail-recursion elimination.
• Compiler does both things automatically!
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Illustrating the example
Normal
behaviour
func2()
func1() func1() func1()
int func1(){
int a, b, c;
if (some_condition)
Optimized return func2();
behaviour else
return 1;
}
func1() func2()
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Eliminate Memory Leaks
• Memory leak occurs when programmers create a memory in heap
and forget to delete it.
• Eventually, in the worst case, too much of the available memory may
become allocated and all or part of the system or device stops
working correctly, the application fails, or the system slows down
vastly.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
More… (self-study)
• Loop unrolling
• Loop jamming
• Loop inversion
• Strength reduction
• Tail recursive call elimination
• etc.
Source: http://icps.u-strasbg.fr/~bastoul/local_copies/lee.html
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus