Code Optimization Techniques

Optimization of code

Optimization of code is done by applying code transformations to improve performance, like execution time, code size, minimum resource utilization etcetera. These transformations can be made either at a high level or at a low level.

In computing, optimization is the process of modifying a system to improve its efficiency. The system can be a single computer program, a collection of computers or even an entire network such as the internet. Good code optimization can make your code run faster and use fewer server resources, and can even have the effect of making code easier to debug and maintain.

The main point of optimization is to improve the code. There are two aspects:

  • Make the program faster (in terms of execution steps)
  • Make the program smaller (in terms of memory)

The following are the ways by which code can be optimized:

  1. Replace an expensive operation by a cheap one. In other words reduce the strength of the operation. For example: x ^ 2 can be written as x * x.
  2. Dead code elimination of source code, such as:
    i) Eliminate code that cannot be reached.
    ii) Eliminate variables whose values are not used.
  3. Remove White Space: White space is required for better understanding of code or better organization of code for developers. These are in no way useful for the visitor of the site since they will not change or improve it. White space should be removed carefully without any side effect on the page of the web site. White space removal (the elimination of superfluous spaces, tabs, new lines, and comments) will result in code size reduction and also in space requirements.
  4. Comments should be removed from the source code because it will increase the download time. Aside from the once widely used META "keywords" tag, most META tag information is just glorified commenting. Information like the META "generated" tag is useless at best and always unnecessarily revealing. Why should one announce to the world that his/her page was created in FrontPage?
  5. Minimize Long Function and Variable Names

    Names of functions and variables should be clear as defined by good programming practice but names that are too lengthy should be avoided. The user does not require the source code of the web site. Only the developers are concerned with the function and variable names. Thus the name should be small and as far as possible should reveal the meaning. Since the browser does not care whether the variable is named gn_ChannelMenu or hj, converting those long names to single or double letter designations for deployment can add up to substantial savings.
  6. If there is some code like x=x+1, it should be changed into x++. Both lines accomplish the same thing but the later one just uses a couple of fewer characters.
  7. In ASP, variable declaration is not compulsory but there are some good reasons to declare the variable.

    i) Variable declaration increases performance: Variables that are used without declaration cause extra processing.
    ii) Variable declaration prevents accidental misuse of them: By declaring variables we can avoid a situation of giving the same name to various variables meant for various purposes. The fact that all variables in ASP are variants (nontyped) contributes to this problem, because two different blocks of code could be working with the data of two different data types and still be able to use the same global variable.
  8. Subs and Functions

    If there are identical or similar code blocks in an application, these code blocks should be converted into Subs or Functions. Subs and Functions can take parameters so it doesn't matter if the code blocks do similar things; parameters can be created that enable the functionality of Subs and Functions to be extended. In addition, by combining similar code blocks into Subs and Functions, it becomes easier to debug and extend the code. Rather than having to find all the similar code blocks and edit each separately, only one Sub or Function needs to be edited.