?? cprofile.pas
字號:
{-----------------------------------------------------------------------------
Unit Name: CProfile
Author: hubdog(陳省)
Email: hubdog@263.net
Purpose: 演示如何使用GpProfile來分析程序運行的性能
History:
2003-4-4 創建本單元
-----------------------------------------------------------------------------}
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 {>>GpProfile U} uses GpProf; {GpProfile U>>}
{$R *.dfm}
//起泡排序
procedure TForm1.BubbleSort(var A: array of Integer);
var
I, J, T: Integer;
begin {>>GpProfile} ProfilerEnterProc(1);
try {GpProfile>>}
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;
{>>GpProfile} finally ProfilerExitProc(1);
end; {GpProfile>>}
end;
procedure TForm1.Button1Click(Sender: TObject);
var
I: Integer;
begin
Screen.Cursor := crHourGlass;
try
//調用三次,以便觀察算法的平均效率
for I := 0 to 2 do
begin
//初始化數組
RandomizeArrays;
//執行三個排序
BubbleSort(BubbleSortArray);
SelectionSort(SelectionSortArray);
QuickSort(QuickSortArray);
end;
finally
Screen.Cursor := crDefault;
end;
end;
//快速排序
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 {>>GpProfile} ProfilerEnterProc(2);
try {GpProfile>>}
QuickSort(A, Low(A), High(A));
{>>GpProfile} finally ProfilerExitProc(2);
end; {GpProfile>>}
end;
//初始化用于排序的數組
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;
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;
end.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -