# 02 intel vtune session 02

Post on 14-Apr-2018

215 views

Embed Size (px)

TRANSCRIPT

7/29/2019 02 Intel VTune Session 02

1/24

Installing Windows XP Professional Using Attended Installation

Slide 1 of 24Ver. 1.0

Code Optimization and Performance Tuning Using Intel VTune

In this session, you will learn to:

Identify time and space complexity

Optimize programming constructs

Identify performance libraries for optimization

Objectives

7/29/2019 02 Intel VTune Session 02

2/24

Installing Windows XP Professional Using Attended Installation

Slide 2 of 24Ver. 1.0

Code Optimization and Performance Tuning Using Intel VTune

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.

Examining Algorithms

7/29/2019 02 Intel VTune Session 02

3/24

Installing Windows XP Professional Using Attended Installation

Slide 3 of 24Ver. 1.0

Code Optimization and Performance Tuning Using Intel VTune

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 theconditions specified in it. As a result, the time complexity of

an algorithm is measurable with reference to the conditions

involved in the algorithm.

Identifying Time Complexity

7/29/2019 02 Intel VTune Session 02

4/24

Installing Windows XP Professional Using Attended Installation

Slide 4 of 24Ver. 1.0

Code Optimization and Performance Tuning Using Intel VTune

Computing the nth number of the fibonacci series:Step 1: start

Step 2: input the value of n

Step 3: if (n < = 1) then go to step 14

Step 4: x = 0

Step 5: y = 1

Step 6: write (x + + y)

Step 7: for (i = 0 to n-1)

Step 8: {f = y + x

Step 9: x = y

Step 10: y = f

Step 11: i = i + 1

Step 12: write (f) }

Step 13: go to step 15Step 14: write (n)

Step 15: stop

Identifying Time Complexity (Contd.)

7/29/2019 02 Intel VTune Session 02

5/24

Installing Windows XP Professional Using Attended Installation

Slide 5 of 24Ver. 1.0

Code Optimization and Performance Tuning Using Intel VTune

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.

Identifying Time Complexity (Contd.)

Statements Frequency o f Execut ion

Step 2: input the value of n 1

Step 3: if (n < = 1) then go to step 14 1

Step 14: write (n) 1

Total Number of instructions executed 3

7/29/2019 02 Intel VTune Session 02

6/24

Installing Windows XP Professional Using Attended Installation

Slide 6 of 24Ver. 1.0

Code Optimization and Performance Tuning Using Intel VTune

If the value of n is greater than 1, time complexity is 4n-2.

This is shown in the following table.

Identifying Time Complexity (Contd.)

Statements Frequency of ExecutionStep 2: input the value of n 1Step 3: if (n < = 1) then go to step 13 1Step 4: x = 0 1Step 5: y = 1 1Step 6: for i= 2 to n-1 repeat steps 7 to 10 1Step 7: f = y + x n-2Step 8: x = y n-2Step 9: y = f n-2Step 10: i = i + 1 n-2Step 11: write (f) 1Total number of instructions executed 4n-2

7/29/2019 02 Intel VTune Session 02

7/24

Installing Windows XP Professional Using Attended Installation

Slide 7 of 24Ver. 1.0

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.

7/29/2019 02 Intel VTune Session 02

8/24

Installing Windows XP Professional Using Attended Installation

Slide 8 of 24Ver. 1.0

Code Optimization and Performance Tuning Using Intel VTune

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.

Identifying Space Complexity

7/29/2019 02 Intel VTune Session 02

9/24

Installing Windows XP Professional Using Attended Installation

Slide 9 of 24Ver. 1.0

Code Optimization and Performance Tuning Using Intel VTune

Refer to the following algorithm:Step 1: start

Step 2: function Add (k, m)

Step 3: l = 0

Step 4: for j= 1 to m repeat step 5

Step 5: l = l + k[j]

Step 6: return the value of lStep 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.

Identifying Space Complexity (Contd.)

7/29/2019 02 Intel VTune Session 02

10/24

Installing Windows XP Professional Using Attended Installation

Slide 10 of 24Ver. 1.0

Code Optimization and Performance Tuning Using Intel VTune

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 theprogramming 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.

Examining Programming Constructs

7/29/2019 02 Intel VTune Session 02

11/24

Installing Windows XP Professional Using Attended Installation

Slide 11 of 24Ver. 1.0

Code Optimization and Performance Tuning Using Intel VTune

Loops can be optimized by:

Removing unwanted parts of loops

Combining loops

Using unrolling

Reducing work inside loopsUsing sentinel values

Looking at the order of loops

Looking at operators

Examining Loops

In this technique, you need to

first identify the

decision-making steps present

inside a loop.

After identifying these steps,

you need to decide whether theloop affects the steps in any

way.

If the loop does not affect the

steps, remove the unaffected

steps from the loop.

When multiple loops work on

the same variables, you may

combine the loops.

This helps reducecomputation time because the

total number of instructions

executed reduces.

Unrolling refers to the process

of breaking compact loops

into simpler statements.

The values of someexpressions, variables, or

constants do not change

inside the loop and

unnecessarily consume

processor time.

Therefore, a better approach

is to bring them outside the

loop.

A sentinel value is a valuethat is placed at the end of a

search range.

Sentinel value prevents you

from performing the additional

task of checking the end of

the input search string.

You can improve the

efficiency of a program by

changing the order of loops.

In terms of efficiency,

operations such as

multiplication and division are

more expensive thanoperations such as addition.

You should try to convert all

expensive operations with

cheaper ones.

7/29/2019 02 Intel VTune Session 02

12/24

Installing Windows XP Professional Using Attended Installation

Slide 12 of 24Ver. 1.0

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.

You can identify fast functions

by knowing the time

complexity associated with

the function.

You need to be selective

about mathematical functions,

such as square root, used in

a program.

You can sequence

mathematical functions based

on execution time.

Use standard mathematical

approaches to compute a

result that uses complex

calculations.This enables you to solve a

problem more efficiently.

Using static functions,

evaluation is faster and

efficiency is improved.

7/29/2019 02 Intel VTune Session 02

13/24

Installing