Performance analysis tool gpprofile

Source: Internet
Author: User

Performance analysis toolsGpprofile

Author Chen Province

Introduction 

Two years ago, when my machine was still amd K6 233 and 1.2 m memory, I tried to use JBuilder and JDK to compile and run the most complex Java program Hello world, at that time, the Java program gave the impression that I could cook a cup of coffee each time during compilation and running, which made me very envious of the Java programmers who work leisurely and enjoy coffee.

One day I started JBuilder after upgrading my antique computer to AMD athlon XP 1700 + and M memory. This time, I was surprised to find that the screen was no longer so crazy that it would make me cry, and the hard disk was no longer so bad that it hurt me-the original JBuilder could run as smoothly as Delphi. After that, I became a fan of garbage collection from virtual machine creators.

Therefore, to succeed in commercial software, performance is undoubtedly very important. The less resources your program occupies and the faster it runs, the more market share you may have. Therefore, a good performance analysis tool is undoubtedly indispensable. The gpprofile is an open-source performance analysis tool developed by primoz Gabriel jelcic. It can analyze the code running speed and help you locate the bottleneck of system performance. The latest version of gpprofile is 1.3.3. The installation file and source code of this software are available on the related CD of this book.

 

Gpprofile features include

Open all source code for easy modification

In addition to executable files, you can also analyze DLL and package files

Multithreading supported

Conditional compilation is supported.

Integrated code Preview

Integrated performance analysis result viewing tool

 

UseGpprofileAnalyze Code Performance

 

The best way to learn new things is to practice it. Delphi provides an example in the demos \ threads directory to demonstrate how to use the thread. In this example, it compares three different sorting methods (Bubble sorting, select sorting and fast sorting) performance differences. This example demonstrates how to use gpprofile to quantitatively compare performance differences between different algorithms.

The following is the code after I modified the Borland example. The main change is to remove the visual display of threads and sorting:

 

{-----------------------------------------------------------------------------
Unit name: cprofile
Author: hubdog (Chen Province)
Email: hubdog@263.net
Purpose: demonstrate how to use gpprofile to analyze program running performance
History:
-4-4 create this unit
-----------------------------------------------------------------------------}

Unit cprofile;

Interface

Uses
Windows, messages, sysutils, variants, classes, graphics, controls, forms,
Dialogs, stdctrls;

Type
Tform1 = Class (tform)
Button1: tbutton;
Procedure button1click (Sender: tobject );
Private
{Private Declarations}
Public
{Public declarations}
Procedure bubblesort (var a: array of integer );
Procedure selectionsort (var a: array of integer );
Procedure quicksort (var a: array of integer );
Procedure randomizearrays;
End;

Type
Tsortarray = array [0 .. 15000] of integer;

VaR
Form1: tform1;
Bubblesortarray, selectionsortarray, quicksortarray: tsortarray;

Implementation

{$ R *. DFM}

// Bubble Sorting

Procedure tform1.bubblesort (var a: array of integer );
VaR
I, j, T: integer;
Begin
For I: = high (a) downto low (a) do
For J: = low (A) to high (a)-1 do
If a [J]> A [J + 1] Then
Begin
T: = A [J];
A [J]: = A [J + 1];
A [J + 1]: = T;
End;
End;

Procedure tform1.button1click (Sender: tobject );
VaR
I: integer;
Begin
Screen. cursor: = crhourglass;
Try
// Call three times to observe the average efficiency of the algorithm
For I: = 0 to 2 do
Begin
// Initialize the Array
Randomizearrays;
// Execute three orders
Bubblesort (bubblesortarray );
Selectionsort (selectionsortarray );
Quicksort (quicksortarray );
End;
Finally
Screen. cursor: = crdefault;
End;
End;

// Quick sorting
Procedure tform1.quicksort (var a: array of integer );

Procedure quicksort (var a: array of integer; ILO, IHI: integer );
VaR
Lo, hi, mid, T: integer;
Begin
Lo: = ILO;
Hi: = IHI;
Mid: = A [(Lo + hi) Div 2];
Repeat
While a [lo] <mid do
INC (LO );
While a [HI]> mid do
Dec (HI );
If Lo <= Hi then
Begin
T: = A [lo];
A [lo]: = A [HI];
A [HI]: = T;
INC (LO );
Dec (HI );
End;
Until lo> Hi;
If HI> ILO then quicksort (A, ILO, hi );
If Lo <IHI then quicksort (A, lo, IHI );
End;

Begin
Quicksort (A, low (A), high ());
End;

// Initialize the Array Used for sorting
Procedure tform1.randomizearrays;
VaR
I: integer;
Begin
Randomize;
For I: = low (bubblesortarray) to high (bubblesortarray) Do
Bubblesortarray [I]: = random (170 );
Selectionsortarray: = bubblesortarray;
Quicksortarray: = bubblesortarray;
End;

// Select sorting
Procedure tform1.selectionsort (var a: array of integer );
VaR
I, j, T: integer;
Begin
For I: = low (A) to high (a)-1 do
For J: = high (a) downto I + 1 do
If a [I]> A [J] Then
Begin
T: = A [I];
A [I]: = A [J];
A [J]: = T;
End;
End;

End.

 

In the above Code, the randomizearrays method is used to initialize the sorting array, while the bubblesort, selectionsort, and quicksort Methods correspond to the Bubble sorting, respectively. Note that in the button1click event, we perform three sorts repeatedly to observe the sorting stability and average efficiency of the sorting algorithm.

 

UseGpprofileAdd Performance Analysis Code

 

The gpprofile installer adds two menu items under the IDE Tools menu of Delphi:

Gpprofile

Gpprofile-Remove Instrumentation

 

After you click the gpprofile menu, the gpprofile automatically loads the project file in the current ide as the test project, as shown below:

 

 

There is an instrumentation page in the main form of gpprofile. on this page, we can see all the procedures and functions of all functions and classes in the test project. After a method is selected, the corresponding code preview is displayed in the edit box below. the checkbox before the method is selected indicates that gpprofile will analyze the performance of the function or process.

 

After selecting, click Project | instrument. gpprofile modifies the source code of the project and adds the gpprofile API Declaration. In this case, we will receive a source code change in Delphi IDE, the message of the source program must be reloaded. After selecting "OK", you will see the modified Code. The Code diagram is as follows:

 

Procedure tform1.selectionsort (var a: array of integer );
VaR
I, j, T: integer;
Begin {> gpprofile} profilerenterproc (3 );
Try {gpprofile >>}
For I: = low (A) to high (a)-1 do
For J: = high (a) downto I + 1 do
If a [I]> A [J] Then
Begin
T: = A [I];
A [I]: = A [J];
A [J]: = T;
End;
{> Gpprofile} finally profilerexitproc (3 );
End; {gpprofile >>}
End;

 

The Code enclosed between the {> gpprofile} {gpprofile >}and {> gpprofile u} {gpprofile U >>}is the performance analysis code of gpprofile.

 

Next, we need to compile and run the modified Code. Note that to compile the program that has added the gpprofile API call, we need to add the installation path of gpprofile to the library path, as shown in the following figure:

Otherwise, the system prompts that gpprof. DCU cannot be found during compilation. After adding a path, click "run. When the application ends, the gpprofile API automatically generates the *. PRF file in the directory where the application is located, which is a record file for analyzing performance.

 

View Test report

If the gpprofile is enabled when the application is running. Then, when the application program ends, gpprofile automatically opens the test report. Otherwise, you should select the profile \ Open menu on the main interface of gpprofile to open the test report in the directory where the application is located.

After opening the test report, we can switch to the Analysis page on the main interface to see the performance report, as shown below:

 

 

The report is divided into the following columns:

Procedure: displays the name of the method we selected for analysis.

% Time: displays the percentage of the running time of each method relative to the total running time.

Time: the number of seconds of the actual running time

CILS: indicates the number of times the analyzed method runs. (Sometimes the performance data provided by one operation may be unstable, and the average value of the result can be obtained from multiple operations)

MIN/call: minimum time required for multiple calls

Max/call: the maximum time used for multiple calls.

AVG/call: average time of each call

 

It can be seen that the effect of fast sorting in the three sorting methods is the best. For arrays with 15000 elements, the speed is 745 times of Bubble sorting, and 253 times of sorting is selected. Imagine that if the algorithms for 30 processes in your Delphi Program are slow algorithms like Bubble sorting, I bet that Java programs using 30 algorithms similar to quick sorting must be 20 times faster than your Delphi Program. It is estimated that your boss will let you cook coffee in another way.

 

To put it bluntly, from the above operations, gpprofile is very simple to use and should be the simplest one I have used in Performance Analyzer. However, it adds many custom tags and API calls to our code. It is inconvenient to manually delete them, fortunately, you can click Project | remove instrumentation to automatically delete the {> gpprofile} and other tags inserted in the code, but to be safe, I suggest you back up the code before removing instrumentation to avoid losses.

 

Summary

Gpprofile is a simple and convenient performance analysis tool and is free and open-source. However, it also has limitations. As a source code performance analysis tool, it cannot analyze projects without source code. Fortunately, this is not a problem. In addition, gpprofile can only tell us how bad the code is, but it won't teach you how to improve the algorithm.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.