131
C H A P T E R 9
Code Optimization
In this chapter, code optimization techniques which often have a major impact on the com-
putational efficiency of C codes are covered. ese techniques include compiler optimizations,
efficient C code writing, and architecture-specific instructions of the ARM processor. For a
better understanding of these techniques, they are illustrated through the signal processing ex-
ample app of linear convolution. In general, to write an efficient C code, it helps to know how
the processor implements it the way it is written.
9.1 CODE TIMING
When attempting to increase code efficiency, it is of great help to find out which segment or
segments are running inefficiently. is can be achieved by manually timing code segment or
segments via a high-resolution system clock. An imprecise way of timing is a time display of the
computer being used. A more precise way is to use C clock functions in the header file time.h
(Android) or mach_time.h (iOS). On Android targets, the function clock_gettime with the time
sources CLOCK_REALTIME and CLOCK_MONOTONIC (utilizing Java clock functions
of System.currentTimeMillis() and System.nanoTime()) provide timing functionality. e example
code segments below indicate how time.h is used for this purpose. For Android:
struct timespec startTime, stopTime;
clock_gettime(CLOCK_MONOTONIC, &startTime);
/*
---Code to be analyzed---
*/
clock_gettime(CLOCK_MONOTONIC, &stopTime);
unsigned long long totalTime = (stopTime.tv_sec - startTime.tv_sec)
* 1000000000LL + stopTime.tv_nsec - startTime.tv_nsec;
//totalTime contains time elapsed in nanoseconds
and for iOS:
clock_t start, finish;
start = clock();
/*
---Code to be analyzed---
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.161.116