LoginSignup
4
0

More than 1 year has passed since last update.

【Delphi】配列 VS リスト 処理速度対決

Posted at

上記記事のDelphi版です。

知りたいと思った理由

最近Delphiに入門したため。

環境

  • Rad Studio 11.3
  • Windows 10
  • VCLアプリケーション
    image.png

条件

  • 1億回の書込と読込を計測
  • 計測は5回平均
  • 読込は、forとforeach(for in)それぞれ計測

結果(ms)

リスト 配列 差分(リスト-配列)
書込 2:430 0:216 2:224
For読込 0:234 0:197 0:037
Foreach読込 0:376 0:278 0:098

所感

リストの読み込みが、意外と高速だと思いました。
Delphiの書き方がおかしかったら、ご指摘ください。

次回

  • Delphiで良く見かけるデータセットも計測
  • TDictionaryを加えて、コレクションからの検索も計測

使用したソースコード

検証ソース
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.Generics.Collections;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private 宣言 }
    const TestCount = 100000000;  // 1億回試行
    procedure WriteLog(const aStartTime: TTime; const aEndTime: TTime; const aMsg:string);
  public
    { Public 宣言 }
  end;

var
 Form1 : TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  arrayTestData : Array Of Integer;
  startTime : TTime;
  endTime : TTime;
begin

  { Array }
  { 準備 }
  System.SetLength(arrayTestData, TForm1.TestCount);

  { 書き込み }
  startTime := System.SysUtils.Time();
  for var i := System.Low(arrayTestData) to System.High(arrayTestData) do
    arrayTestData[i] := i;
  endTime := System.SysUtils.Time();
  Self.WriteLog(startTime, endTime, '書込時間 array');

  { 読み込み(for) }
  startTime := System.SysUtils.Time();
  for var i := System.Low(arrayTestData) to System.High(arrayTestData) do
    var _ := arrayTestData[i];
  endTime := System.SysUtils.Time();
  Self.WriteLog(startTime, endTime, '読込時間(for) array');

  { 読み込み(foreach) }
  startTime := System.SysUtils.Time();
  for var d in arrayTestData do
    var _ := d;
  endTime := System.SysUtils.Time();
  Self.WriteLog(startTime, endTime, '読込時間(foreach) array');

  { List }
  { 準備 }
  var listTestData := TList<Integer>.Create();

  { 書き込み }
  for var i := 0 to TForm1.TestCount - 1 do // LowとHigh使えない?
    listTestData.Add(i);
  endTime := System.SysUtils.Time();
  Self.WriteLog(startTime, endTime, '書込時間 list');

  { 読み込み(for) }
  startTime := System.SysUtils.Time();
  for var i := 0 to listTestData.Count - 1  do // LowとHigh使えない?
    var _ := listTestData[i];
  endTime := System.SysUtils.Time();
  Self.WriteLog(startTime, endTime, '読込時間(for) list');

  { 読み込み(foreach) }
  startTime := System.SysUtils.Time();
  for var d in listTestData do
    var _ := d;
  endTime := System.SysUtils.Time();
  Self.WriteLog(startTime, endTime, '読込時間(foreach) list');


end;

procedure TForm1.WriteLog(const aStartTime: TTime; const aEndTime: TTime; const aMsg:string);
begin
  var durationTime := FormatDateTime('hh:nn:ss:zzz', aEndTime - aStartTime);
  Memo1.Lines.Add(Format('%d件 %s | %s',[TForm1.TestCount, aMsg, durationTime]));
end;

end.

以上

4
0
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0