Snippets

Stefan Glienke Compare managed objects of Spring4D and Mitov

Created by Stefan Glienke last modified
program Project1;

{$APPTYPE CONSOLE}

uses
  Classes,
  Diagnostics,
  SysUtils,
  Mitov.Attributes, Mitov.Types,
  Spring;

type
  TFoo = class
  private
    fCount: Integer;
    fCount2: Integer;
    fCount3: Integer;
    fStrings: TStrings;
    fIntf: IInterface;
  public
    constructor Create;
    destructor Destroy; override;

    property Count: Integer read fCount;
    property Count2: Integer read fCount2;
    property Count3: Integer read fCount3;
    property Strings: TStrings read fStrings;
    property Intf: IInterface read fIntf;
  end;

  TSpringFoo = class(TManagedObject)
  private
    [Default(42)]
    fCount: Integer;

    [Default(42)]
    fCount2: Integer;

    [Default(42)]
    fCount3: Integer;

    [Managed(TStringList)]
    fStrings: TStrings;

    [Managed(TInterfacedObject)]
    fIntf: IInterface;
  public
    property Count: Integer read fCount;
    property Count2: Integer read fCount2;
    property Count3: Integer read fCount3;
    property Strings: TStrings read fStrings;
    property Intf: IInterface read fIntf;
  end;

  TMitovFoo = class(TBasicObject)
  private
    [Default(42)]
    fCount: Integer;

    [Default(42)]
    fCount2: Integer;

    [Default(42)]
    fCount3: Integer;

    [AutoManage(TStringList)]
    fStrings: TStrings;

    [AutoManage(TInterfacedObject)]
    fIntf: IInterface;
  public
    property Count: Integer read fCount;
    property Count2: Integer read fCount2;
    property Count3: Integer read fCount3;
    property Strings: TStrings read fStrings;
    property Intf: IInterface read fIntf;
  end;

{ TFoo }

constructor TFoo.Create;
begin
  inherited Create;
  fCount := 42;
  fCount2 := 42;
  fCount3 := 42;
  fStrings := TStringList.Create;
  fIntf := TInterfacedObject.Create;
end;

destructor TFoo.Destroy;
begin
  fStrings.Free;
  inherited;
end;

const
  MAXCOUNT = 100000;
  THREADCOUNT = 1;

procedure TestFoo;
var
  i: Integer;
  f: TFoo;
begin
  for i := 1 to MAXCOUNT do
  begin
    f := TFoo.Create;
    f.Free;
  end;
end;

procedure TestSpring;
var
  i: Integer;
  f: TSpringFoo;
begin
  for i := 1 to MAXCOUNT do
  begin
    f := TSpringFoo.Create;
    f.Free;
  end;
end;

procedure TestMitov;
var
  i: Integer;
  f: TMitovFoo;
begin
  for i := 1 to MAXCOUNT do
  begin
    f := TMitovFoo.Create;
    f.Free;
  end;
end;

procedure Test(proc: TProcedure; const text: string);
var
  sw: TStopwatch;
  threads: array[1..THREADCOUNT] of TThread;
  i: Integer;
begin
  for i := 1 to THREADCOUNT do
  begin
    threads[i] := TThread.CreateAnonymousThread(proc);
    threads[i].FreeOnTerminate := False;
  end;

  sw := TStopwatch.StartNew;
  for i := 1 to THREADCOUNT do
    threads[i].Start;

  for i := 1 to THREADCOUNT do
  begin
    threads[i].WaitFor;
    threads[i].Free;
  end;

  Writeln(text, ': ', sw.ElapsedMilliseconds, ' ms');
end;

begin
  try
    Writeln('testing creating and destroying ', MAXCOUNT, ' instances in ', THREADCOUNT, ' threads');
    Writeln;
    Test(TestFoo, 'native compiled');
    Test(TestSpring, 'Spring4D');
    Test(TestMitov, 'Mitov');
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  ReportMemoryLeaksOnShutdown := True;
  Readln;
end.

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.