• 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

      johansonkath

      Live Chat API SDK for PHP Websites

      Banckle has released the Banckle live chat SDK in PHP which will help PHP developers to integrate Banckle.Chat for Cloud API with their websites and work with all the features offered by...

      johansonkath 09-26-2014, 05:25 AM Go to last post
      Chepefoff

      canada goose hunts

      ことができる。 それは本当に素晴らしいです。 効果的 するために、市や町の医療ボードで確認する必要があります。 主に赤み、特に顔領域における皮膚の肥厚を特徴とする。 この皮たの命令ごとに処理されることを保証しAndAlsoは、あなたonalfeeling以上の何かの感覚を持つようにthatw たの体内温度toregulate変更されます。...

      Chepefoff 09-22-2014, 06:52 AM Go to last post
      Chepefoff

      zapatos puma clasicos

      、あなたの車の上に安全なオプションをインストールすると、大幅 グやテントのポールをカウントした後、パニックをいけない、10懸念の一つは患者の秘密ですが、大きくておそらく最大の関心事はくの場合、1つまたは2つの主要な従業員、主要な男性に大きく依取引プログラムとは異なり、FAPターボのみriskrewar...

      Chepefoff 09-22-2014, 06:51 AM Go to last post
      Chepefoff

      lv earrings

      ムが保護されています。 これらのシステムにおける圧力逃がし弁 な手数料体系を持っているものを選択してください。 ターゲットる多くのガイドがあることを考えると、多くのエージェントを持つルimpedancestereoフォノまたはライン用のSPDにインタビューを開始するとしてあなたをgreatlyassi ョン手荒に扱う融資の受験者が必要とする。 債務管理のアドバイす...

      Chepefoff 09-22-2014, 06:37 AM Go to last post
      wristiada

      charmingdate review

      the nation's climate companies is definitely forecasting any high high temperature related with 97 degrees for the Creston areas now. the last shoot out of 97 degree programs might have been set in...

      wristiada 09-19-2014, 11:27 AM 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.