⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cprofile.pas

📁 Delphi深度探索,Delphi深度探索(第二版)
💻 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 + -