• Loading
    • Optimization Techniques In C

      Optimizing code improves the overall performance of an application when an end user runs it. When working in C, you use compilers to optimize code. You can also optimize code written in C by using techniques, such as loop unrolling, loop jamming, and loop invariants.
      This Article describes various optimization techniques in C. It explains how to optimize macros, initializations, and data structures in C and how to remove invariants and inefficiencies in loops.
      To optimize a program written in C, you need to consider its execution time. You can calculate the amount of time taken for each function to execute by using the following features available in C:

      • Stdio function: Uses the getc() and putc() macros, which function faster than user-defined function calls.
      • memcpy() function: Reduces execution time.
      • Single precision operator: Increases the speed of operation.



      In C, you can also optimize the code during program compilation. To do so, you need to know the execution time of operators and library functions such as *, +, and strcpy(). All operators and functions do not take the same amount of time to execute.

      Some operators and library functions available in C with their execution time are as follows:-

      Operator/Library Function Time (in microsec)
      Int-int 0.013
      deref pointer 0.025
      array index 0.026
      Int = 0.036
      Bit shift 0.037
      (int) float 0.038
      float * float 0.049
      float + float 0.049
      Int ^ int 0.050
      Int * int 0.198
      srtcpy() 0.665
      strcmp() 0.556
      Malloc 2.834


      Some operators, such as Int–int and array index, take less time, and some operations take longer time to execute. For example, multiplication of integers takes longer than subtraction.

      Memory hierarchy also affects the performance. If you want your program to run fast, you need to assign memory at the top of the hierarchy and you need to know how the memory hierarchy works.

      The order in which you should use memory in a program to improve speed is:

      • CPU registers: A storage device that you use to access data.
      • RAM: Permanent memory that you use to access data.
      • ROM: Temporary memory that is slower than RAM.
      • Virtual Memory: A very slow temporary memory, which takes thousands of clock cycles to access data.


      [P.S: Clock cycle is the time between two pulses of a clock in memory. Some processors can execute one instruction per clock cycle while others can execute multiple instructions per clock cycle.]

      You need to identify the performance of algorithms to optimize the code in C. The most commonly-used method to measure the performance of the algorithm is the O-notation. The O-notation is a formula that calculates resource usage.

      Techniques for Optimization
      There are three types of techniques for optimizing code in C:

      • Compute-bound techniques
      • Memory-bound techniques
      • Input/Output-bound techniques


      Compute-bound techniques involve computing the time taken to execute operators and functions. Compute-bound techniques include:

      • Profiling
      • Inlining
      • Loop unrolling
      • Loop jamming
      • Loop invariants computation
      • Loop inversion
      • Tail recursion elimination
      • Table look up



      In memory-bound computations, you need to take into account the memory that the program uses. The use of memory from the lower parts of the memory hierarchy increases the execution time. You need to use memory from the appropriate level while running programs. Memory-bound techniques include:

      • Locality of reference
      • Row-Major addressing
      • Padding reduction
      • Memory leaks


      In Input/Output (I/O) bound optimization, the sequential access and random access methods are used to reduce the time required to retrieve information.

      A buffered I/O is faster than an unbuffered I/O. You can use read() and write() functions with large buffers. You can use mmap() to reduce time required to retrieve information. Mmap() maps the process’s address space with shared memory or file.


      Other Optimization Techniques
      You can also optimize code by rewriting time-critical functions in assembly language programming. Changing the program in C to assembly language is difficult.You can change only the critical functions into assembly code.

      To optimize the code in C, use a compiler to generate an assembly language. You can also optimize your code by changing the source of the hardware, such as upgrading the CPU, adding a co-processor, changing Internet connection speed, and installing a high-speed RAID disk array.

      You can install compilers from several vendors to compile programs with time-critical functions and check which compiler is faster.

      You can avoid using increment and decrement operators, such as ++ and –, in loops. Some other ways to increase speed are:

      • Minimize the use of global variables in the program.
      • Declare all functions other than global variables as static within the file.
      • Use word size variables such as int and float instead of char, short, and double.
      • Avoid using recursion.
      • Avoid using the sqrt() function as it is CPU intensive.
      • Use single-dimensional arrays.
      • Do not split closely related functions into separate files.
      • Use puts() function instead of the printf function.
      • Use unformatted/binary file access instead of formatted file access.
      • Use mallopt() if the compiler supports this function to control malloc function.
      • Use macros instead of small functions to save CPU time.
    • Enter your email address:

      Delivered by FeedBurner

    • Latest Posts

      kate01

      Re: Transfer iPhone SMS and MMS to Android

      You can transfer SMS messages to Android devices manually and freely by following the steps shared in this guide: goo.gl/L6rZNS

      kate01 06-29-2015, 03:51 AM Go to last post
      Moveblelago

      Re: Nuestra historia

      “现在微博、朋友圈往往都是负面消息发酵的很快,但今天这位老人
      而在面对被栽赃成为杀妻凶手悬疑迷雾时,他又必须在悬疑色彩重重

      Moveblelago 06-14-2015, 07:05 PM Go to last post
      Moveblelago

      Re: hi I join Tech Forum

      理改编,将幼儿园段落单拿出来组成一部电影,删去了原作中很多败
      苦难学说”在该剧中逐渐升级,孙家兄弟的平凡生活将再次遭受磨难

      Moveblelago 06-14-2015, 04:46 PM Go to last post
      Moveblelago

      Re: hi I join Tech Forum

      众。梁文道户外走读遭“炮轰” 接受意见反“嘲”高晓松发布会上
      日探班《霍比特人2》片场,采访甘道夫扮演者伊恩·迈凯伦,越来

      Moveblelago 06-14-2015, 04:33 PM Go to last post
      Moveblelago

      Re: hi I join Tech Forum

      局。几乎场场爆满,其中三线城市上座率达到86%,首周末票房轻
      平凡的世界》的创作态度严谨,从筹备到制作超过七年。剧组除了赶

      Moveblelago 06-14-2015, 04:11 PM Go to last post



    Disclaimer: Users of techforum4u.com are responsible for ensuring that any material they post (article, blog posts, images or other mulitimedia content) does not violate or infringe upon the copyright, patent, trademark, or any personal or proprietary rights of any third party, and is posted with the permission of the owner of such rights.Anyone who violates these rules may have their access privileges removed without warning.