Snippets
Created by
Stefan Glienke
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | program ManagedObjectSpeedTest;
{$APPTYPE CONSOLE}
uses
Classes,
Diagnostics,
SysUtils,
Mitov.Attributes, Mitov.Types,
Spring;
type
TCompiledFoo = 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;
{ TCompiledFoo }
constructor TCompiledFoo.Create;
begin
inherited Create;
fCount := 42;
fCount2 := 42;
fCount3 := 42;
fStrings := TStringList.Create;
fIntf := TInterfacedObject.Create;
end;
destructor TCompiledFoo.Destroy;
begin
fStrings.Free;
inherited;
end;
const
MAXCOUNT = 10000;
THREADCOUNT = 1;
type
Test = record
class procedure Run<T: class, constructor>; static;
end;
class procedure Test.Run<T>;
var
sw: TStopwatch;
threads: array[1..THREADCOUNT] of TThread;
i: Integer;
begin
for i := 1 to THREADCOUNT do
begin
threads[i] := TThread.CreateAnonymousThread(
procedure
var
i: Integer;
o: T;
begin
for i := 1 to MAXCOUNT do
begin
o := T.Create;
o.Free;
end;
end);
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(T.ClassName, ': ', sw.ElapsedMilliseconds, ' ms');
end;
begin
try
Writeln('testing creating and destroying ', MAXCOUNT, ' instances in ', THREADCOUNT, ' threads');
Writeln;
Test.Run<TCompiledFoo>;
Test.Run<TSpringFoo>;
Test.Run<TMitovFoo>;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
ReportMemoryLeaksOnShutdown := True;
Readln;
end.
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.