0% found this document useful (0 votes)
3 views

Module 14 - part 3 - Performance Efficiency of a C Program

Module 14 - part 3 focuses on the performance efficiency of C programs, detailing methods for measuring execution time and improving performance. Key strategies include using macros, in-line functions, compiler optimization flags, and choosing better algorithms, while also addressing stack usage and memory management. The module emphasizes the importance of optimizing code to enhance execution speed and reduce resource consumption.

Uploaded by

JAYESH HARLALKA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Module 14 - part 3 - Performance Efficiency of a C Program

Module 14 - part 3 focuses on the performance efficiency of C programs, detailing methods for measuring execution time and improving performance. Key strategies include using macros, in-line functions, compiler optimization flags, and choosing better algorithms, while also addressing stack usage and memory management. The module emphasizes the importance of optimizing code to enhance execution speed and reduce resource consumption.

Uploaded by

JAYESH HARLALKA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Module 14 – part 3:

Performance Efficiency of a C Program


BITS Pilani Dr. Amitesh Singh Rajput
Pilani Campus
Department of Computer Science & Information Systems
Module Overview

• Measuring execution time of a C Program


• Improving Performance of a C Program

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus

Measuring Execution Time


Measuring execution time of a
C program
#include <time.h>
int main(){
time_t start, end;
double diff;
start = time(NULL);
/*
… code goes here …
*/
end = time(NULL);
diff = difftime(end, start);
printf(“The total execution time is: %d”, diff);
}

Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus

Improving Performance of a C Program


Ways to improve C Program
performance
• Macros
• Iterative vs Recursive Program
• In-line functions
• Compilation Flags: -o1, -o2, -o3, -ofast
• Choose better algorithms
• Minimizing Stack Usage
• Eliminate Memory Leaks

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;
}

• Macros improve the performance of the program by reducing stack allocation


overhead that happens in function calls
• Each function call results in a push in the activation stack
• The corresponding return is a pop from the stack

• Macros perform code replacement during the pre-processor stage


• No function call at run-time

• So, wherever possible, we can replace function calls with macros


• Recursive functions can’t be replaced!
• Arguments passed are substituted as it is.
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
Stack behavior: Iterative vs
Recursive
long int fact(int n) long int factr(int n)
{ {
int t, ans;
ans = 1; int ans;
for(t=1; t<=n; t++) if(n==1) return(1);
ans = ans*t; answer = factr(n-1)*n;
return(ans); return(ans);
} }

factr(1)
factr(2)
factr(3)
fact() factr(4)

Stack overhead of Stack overhead of


iterative function call recursive function call
Using iterative program instead of recursive program reduces the function call and stack overhead
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
In-line functions
• In-line function are those function whose definitions are small and
be substituted at the place where its function call is happened.
• Function substitution is totally compiler choice.
• Compiler inserts the function code at the address of each
function call, thereby saving the overhead of a function call.

• Semantics is same as functions instead of macros except no call


and return actions taken.

• 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;
}

Using static keyword before inline forces the compiler to consider an


inline function in the linker.

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

Example usage: gcc myprog.c -ofast

 These are various levels of optimizations, of which -ofast gives


highest performance efficiency.
 For each flag, a pre-defined set of optimizations are performed
like function inlining, tail-recursive call optimization, loop unrolling,
loop jamming, loop inversion, etc.

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:

Slow algorithm Replace with


sequential search binary search or hash lookup
insertion or bubble sort quick sort, merge sort, radix sort

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.

• What causes such issues?


• Having large arrays as local variables.
• Solution: rewrite the code using a static or global array, or
perhaps allocate it from the heap.
• Large structs as locals or parameters.
• Similar solution as above

• Turning recursive functions into iterative also reduces stack overhead.

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.

• The consequences of memory leak is that it reduces the performance


of the computer by reducing the amount of available memory.

• 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.

• Solution: Manage heap space wisely. Free up the allocated memory


when it is not required further.

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

You might also like