Вы находитесь на странице: 1из 24

Installing Windows XP Professional Using Attended Installation Code Optimization and Performance Tuning Using Intel VTune

Objectives

In this session, you will learn to:


Identify time and space complexity Optimize programming constructs Identify performance libraries for optimization

Ver. 1.0

Slide 1 of 24

Installing Windows XP Professional Using Attended Installation Code Optimization and Performance Tuning Using Intel VTune

Examining Algorithms

An algorithm is a set of finite steps that accomplish a specific task. The efficiency of an algorithm is measured in terms of the processor time and the memory space that the algorithm utilizes. It is necessary to tune the algorithm to make optimal use of available resources, such as processor time and memory. To analyze algorithmic complexity, it is important to understand the time and space aspects of complexity.

Ver. 1.0

Slide 2 of 24

Installing Windows XP Professional Using Attended Installation Code Optimization and Performance Tuning Using Intel VTune

Identifying Time Complexity

The time taken to compute the steps involved in an algorithm is called the time complexity of that algorithm. The time taken to execute all the steps for a worst-case scenario is the time complexity of the entire algorithm. The execution of steps in an algorithm depends on the conditions specified in it. As a result, the time complexity of an algorithm is measurable with reference to the conditions involved in the algorithm.

Ver. 1.0

Slide 3 of 24

Installing Windows XP Professional Using Attended Installation Code Optimization and Performance Tuning Using Intel VTune

Identifying Time Complexity (Contd.)

Computing the nth number of the fibonacci series:


Step Step Step Step Step Step Step Step Step Step Step Step Step Step Step 1: start 2: input the value of n 3: if (n < = 1) then go to step 14 4: x = 0 5: y = 1 6: write (x + + y) 7: for (i = 0 to n-1) 8: {f = y + x 9: x = y 10: y = f 11: i = i + 1 12: write (f) } 13: go to step 15 14: write (n) 15: stop

Ver. 1.0

Slide 4 of 24

Installing Windows XP Professional Using Attended Installation Code Optimization and Performance Tuning Using Intel VTune

Identifying Time Complexity (Contd.)

Based on the value of n, two cases can exist in the preceding algorithm. If the value of n is less than or equal to 1, time complexity is constant and does not depend on input, which is n, as shown in the following table.
Statements
Step 2: input the value of n Step 3: if (n < = 1) then go to step 14 Step 14: write (n) Total Number of instructions executed

Frequency of Execution
1 1 1 3

Ver. 1.0

Slide 5 of 24

Installing Windows XP Professional Using Attended Installation Code Optimization and Performance Tuning Using Intel VTune

Identifying Time Complexity (Contd.)

If the value of n is greater than 1, time complexity is 4n-2. This is shown in the following table.
Statements
Step 2: input the value of n Step 3: if (n < = 1) then go to step 13 Step 4: x = 0 Step 5: y = 1 Step 6: for i= 2 to n-1 repeat steps 7 to 10 Step 7: f = y + x Step 8: x = y Step 9: y = f Step 10: i = i + 1 Step 11: write (f) Total number of instructions executed

Frequency of Execution
1 1 1 1 1 n-2 n-2 n-2 n-2 1 4n-2

Ver. 1.0

Slide 6 of 24

Installing Windows XP Professional Using Attended Installation Code Optimization and Performance Tuning Using Intel VTune

Just a minute

What do you mean by the time complexity of an algorithm?

Answer:
The time taken to compute the steps involved in an algorithm is called the time complexity of that algorithm.

Ver. 1.0

Slide 7 of 24

Installing Windows XP Professional Using Attended Installation Code Optimization and Performance Tuning Using Intel VTune

Identifying Space Complexity

Space complexity is the amount of memory that a program requires to accomplish a task. Space complexity is a criterion to measure the efficiency of a program. For example, consider the following expression: Return [a + b + b * c + (a +b c) / (a + b) + 4.0]
If variables b and c are each of 2 bits, the storage requirements for the preceding instruction will be constant. If the values of a, b, and c are taken from user input, the storage space required will vary.

Ver. 1.0

Slide 8 of 24

Installing Windows XP Professional Using Attended Installation Code Optimization and Performance Tuning Using Intel VTune

Identifying Space Complexity (Contd.)

Refer to the following algorithm:


Step Step Step Step Step Step Step 1: start 2: function Add (k, m) 3: l = 0 4: for j= 1 to m repeat step 5 5: l = l + k[j] 6: return the value of l 7: stop

For the preceding algorithm:


The space taken by the k array is m units. The other variables, m, j, and l will take only one unit of space each. As a result, you can obtain the space complexity of this algorithm by adding the space utilized by all the variables.

Ver. 1.0

Slide 9 of 24

Installing Windows XP Professional Using Attended Installation Code Optimization and Performance Tuning Using Intel VTune

Examining Programming Constructs

An application developed in any programming language, such as C, C++, C#, or Java, is based on an algorithm. Each algorithm consists of several programming constructs, such as loops, decisions, and functions. The performance of an application is related to the programming constructs used in the application. To achieve the desired level of optimization, it is important to examine the loops, branching statements, and function calls used in the program.

Ver. 1.0

Slide 10 of 24

Installing Windows XP Professional Using Attended Installation Code Optimization and Performance Tuning Using Intel VTune

Examining Loops

Loops can be optimized by:

Removing unwanted parts of loops Combining loops Using unrolling Reducing work inside loops Using sentinel values Looking at the order of loops Looking at operators

In this technique, you need to first identify the When multiple loops work on decision-making steps present the same variables, you may inside a loop. Unrolling refers to the process combine the loops. of breaking compact loops After identifying these steps, The values of some This helps reduce into simpler you need to statements. decide whether the expressions, variables, or the computation time because loop affects the steps in any A sentinel value is a value constants do not change total number of instructions way. that is the placed at the end of a inside loop and You can improve the executed reduces. search range. If the loop does not affect unnecessarily consume efficiency of a program by the steps, remove the unaffected Sentinel value prevents you In terms of efficiency, processor time. changing the order of loops. steps from a the loop. from performing the additional operations such as approach Therefore, better task checking the endthe of multiplication and division are is to of bring them outside the input search than string. more expensive loop. operations such as addition. You should try to convert all expensive operations with cheaper ones.

Ver. 1.0

Slide 11 of 24

Installing Windows XP Professional Using Attended Installation Code Optimization and Performance Tuning Using Intel VTune

Examining Functions

As the frequency of calling a function increases, program execution time increases. Examining functions is important for performance optimization. Functions can be optimized by:

Using faster functions Identifying mathematical functions Identifying standard functions Declaring local functions as static

Try to use only fast functions. Youneed can identify fast functions You to be selective by knowing the time about mathematical functions, complexity associated with Use standard mathematical such as square root, used in the function. approaches to compute a a program. Using static functions, result that uses complex You can sequence evaluation is faster and calculations. mathematical functions based efficiency is improved. This enables you to solve a on execution time. problem more efficiently.

Ver. 1.0

Slide 12 of 24

Installing Windows XP Professional Using Attended Installation Code Optimization and Performance Tuning Using Intel VTune

Examining Branching

Transferring control from one part of code to another is achieved through branching. Various techniques can be adopted to make the process of branching effective and improve the efficiency of code. Branches can be examined by:

Removing the else clause Using effective case statements Replacing conditional computations

Using an else clause with every if loop leads to You shouldbranching. use effective case ineffective statements in such a way so Therefore, you should try to You can save computational the order of options is defined remove the else clause, if time by replacing conditional based on the frequency of possible. computation with equivalent using the options. arithmetic expressions.

Ver. 1.0

Slide 13 of 24

Installing Windows XP Professional Using Attended Installation Code Optimization and Performance Tuning Using Intel VTune

General Guidelines for Optimization

Some guidelines to follow when writing code are:


Identify optimization areas Identify the depth of optimization Identify correct alternatives Identify what is being asked

Ver. 1.0

Slide 14 of 24

Installing Windows XP Professional Using Attended Installation Code Optimization and Performance Tuning Using Intel VTune

Common Misconceptions About Optimization

Some of the common misconceptions about code and their optimization are:
To think a program doesnt require optimization because it appears to be quite fast. To think that only the optimization performed by the compiler is enough. To believe that short code is efficient. To think a specific solution will be effective without verifying performance results. To think optimizing while programming is good practice.

Ver. 1.0

Slide 15 of 24

Installing Windows XP Professional Using Attended Installation Code Optimization and Performance Tuning Using Intel VTune

Just a minute

What is unrolling? What is sentinel value?

Answer:
Breaking compact loops into simpler statements is called unrolling. A sentinel value is a value that is placed at the end of a search range. This assures the termination of the search.
Ver. 1.0

Slide 16 of 24

Installing Windows XP Professional Using Attended Installation Code Optimization and Performance Tuning Using Intel VTune

Using Performance Libraries For Applications

Each software developer has an individualistic style of writing code. It is possible that the written code will not be efficient. Sections of software, such as the menu bar, remain common in most software. Writing these sections of codes repeatedly makes the process time-consuming and error-prone. In such situations, you might prefer to use existing code, which is called a performance library. This piece of code is already checked for errors. In addition, this code attains a high level of optimization over a period.

Ver. 1.0

Slide 17 of 24

Installing Windows XP Professional Using Attended Installation Code Optimization and Performance Tuning Using Intel VTune

Using Performance Libraries For Applications (Contd.)

The advantages of using performance libraries are the following:


Enable programmers to develop code in less time Offer error free codes Enables best use of resources Help in performance improvement Add to stability of the functioning software application

Ver. 1.0

Slide 18 of 24

Installing Windows XP Professional Using Attended Installation Code Optimization and Performance Tuning Using Intel VTune

Identifying the Types of Performance Libraries

Based on these tasks, performance libraries can be categorized as:

Engineering and science libraries Math libraries Graphic libraries Audio/Video libraries Image processing libraries Other libraries

These libraries are mainly used in scientific and These libraries deal with the engineering applications. evaluation of complex These libraries help draw may include mathematical functions, such graphics, pie charts, graphs, functionalities for searching, as vector and matrix These libraries help optimize and bar and diagrams correctly sorting, evaluating calculations. various functions related to and accurately. various expressions. These libraries help process audio-visual data. images faster. may include Other libraries functionalities for performing various tasks, such as speech recognition, signal processing, and cryptography.

Ver. 1.0

Slide 19 of 24

Installing Windows XP Professional Using Attended Installation Code Optimization and Performance Tuning Using Intel VTune

Just a minute

Which libraries may include functionalities for searching, sorting, and evaluating various expressions?

Answer:
Engineering and science libraries

Ver. 1.0

Slide 20 of 24

Installing Windows XP Professional Using Attended Installation Code Optimization and Performance Tuning Using Intel VTune

Summary

In this session, you learned that:


Time complexity is the time taken by the steps of an algorithm to execute. Space complexity is the measurement of the space utilized by the components of an algorithm. Identifying the data structures used in an algorithm and then adding them can measure space complexity. Loops are generally the most time-consuming constructs of a program. Some of the techniques to optimize loops are: Removing unwanted parts of loops Combining loops Using Unrolling Reducing work inside loops

Ver. 1.0

Slide 21 of 24

Installing Windows XP Professional Using Attended Installation Code Optimization and Performance Tuning Using Intel VTune

Summary (Contd.)

Using Sentinel Values Looking at the order of loops Looking at operators Some of the techniques to optimize functions are: Using faster functions Identifying mathematical functions Identifying standard functions Declaring local functions as static Transferring control from one part of a code to another is achieved through branching.

Ver. 1.0

Slide 22 of 24

Installing Windows XP Professional Using Attended Installation Code Optimization and Performance Tuning Using Intel VTune

Summary (Contd.)

Some of the techniques to make the process of branching effective are: Removing the else clause Using Effective Case Statements Replacing Conditional Computation You should keep the following optimization guidelines in mind when writing codes: Identify optimization areas Identify the depth of optimization Identify correct alternatives Identify what is being asked

Ver. 1.0

Slide 23 of 24

Installing Windows XP Professional Using Attended Installation Code Optimization and Performance Tuning Using Intel VTune

Summary (Contd.)

Some of the common misconceptions about optimization are: A fast program does not require optimization Optimization performed by the compiler is enough A short code is efficient Optimizing while programming is good practice Performance libraries can improve the performance of an application to a large extent. A variety of performance libraries are available for different purposes, such as mathematical, graphical, and task-based functions.

Ver. 1.0

Slide 24 of 24

Вам также может понравиться