home *** CD-ROM | disk | FTP | other *** search
- unit WMain;
-
- interface
-
- uses
- PSTimer,
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- StdCtrls, ExtCtrls;
-
-
- type
- TFMain = class(TForm)
- paBottom: TPanel;
- btTest: TButton;
- paMain: TPanel;
- meOutput: TMemo;
- btClear: TButton;
- chMultipleThreads: TCheckBox;
- btTrace: TButton;
- procedure btTestClick(Sender: TObject);
- procedure btClearClick(Sender: TObject);
- procedure btTraceClick(Sender: TObject);
- private
- procedure WriteHeader;
- procedure WriteTime( const msg : string; time : TTime ); overload;
- procedure WriteRatio( const msg : string; time1, time2 : TTime );
- procedure WriteLn( const msg : string = '' ); overload;
- public
- end;
-
- procedure WriteLn( const msg : string = '' ); overload;
-
- var
- FMain: TFMain;
-
- implementation
-
- {$R *.DFM}
-
- uses
- TraceableClass,
- FastObjects;
-
- const
- COUNT = 1000;
-
- var
- intsInStack : array[0..COUNT-1] of TIntegerRec;
- intsInFastStack : array[0..COUNT-1] of TFastIntegerRec;
- intsInHeap : array[0..COUNT-1] of TInteger;
- ints : array[0..COUNT-1] of Integer;
-
- procedure WriteLn( const msg : string = '' ); overload;
- begin
- FMain.WriteLn( msg );
- end;
-
- procedure TFMain.btTestClick(Sender: TObject);
- var
- i : Integer;
- counter : TPSTimer;
- stackCreateTime : TTime;
- stackFastCreateTime : TTime;
- heapCreateTime : TTime;
- intCreateTime : TTime;
- stackDestroyTime : TTime;
- stackFastDestroyTime : TTime;
- heapDestroyTime : TTime;
- intDestroyTime : TTime;
- overhead : TTime;
- begin
- // Simulate the effects of multiple threads in memory allocation and
- // deallocation?
- IsMultiThread := chMultipleThreads.Checked;
-
- // *************************************************************
- // Calculate overhead
- StartCount(counter);
- for i := 0 to COUNT-1 do
- ;
- overhead := StopCount(counter);
-
- // *************************************************************
- // Time construction
- StartCount(counter);
- for i := 0 to COUNT-1 do
- CreateObject( intsInStack[i], i );
- stackCreateTime := StopCount(counter) - overhead;
- // Check that objects have been adequately created in the stack
- for i := 0 to COUNT-1 do
- Assert( GetObject(intsInStack[i]).Value = i );
-
- StartCount(counter);
- for i := 0 to COUNT-1 do
- CreateObject( intsInFastStack[i], i );
- stackFastCreateTime := StopCount(counter) - overhead;
- // Check that objects have been adequately created in the stack
- for i := 0 to COUNT-1 do
- Assert( GetObject(intsInFastStack[i]).Value = i );
-
- StartCount(counter);
- for i := 0 to COUNT-1 do
- intsInHeap[i] := TInteger.Create( i );
- heapCreateTime := StopCount(counter) - overhead;
-
- // Time insteger "construction", that is, assignment
- StartCount(counter);
- for i := 0 to COUNT-1 do
- ints[i] := i;
- intCreateTime := StopCount(counter) - overhead;
-
- // *************************************************************
- // Time destruction
- StartCount(counter);
- for i := 0 to COUNT-1 do
- FreeObject(intsInStack[i]);
- stackDestroyTime := StopCount(counter) - overhead;
-
- StartCount(counter);
- for i := 0 to COUNT-1 do
- FreeObject(intsInFastStack[i]);
- stackFastDestroyTime := StopCount(counter) - overhead;
-
- StartCount(counter);
- for i := 0 to COUNT-1 do
- intsInHeap[i].Free;
- heapDestroyTime := StopCount(counter) - overhead;
-
- intDestroyTime := 0.0;
-
- // *************************************************************
- // Report results
- WriteHeader;
- // Creation results
- WriteLn( '-------------- CONSTRUCTION' );
- WriteTime( 'Creation in stack', stackCreateTime );
- WriteTime( 'Fast creation in stack', stackFastCreateTime );
- WriteTime( 'Creation in heap', heapCreateTime );
- WriteTime( 'Direct Integer assignment', intCreateTime );
- WriteRatio( 'RATIO stack vs. heap',
- stackCreateTime, heapCreateTime );
- WriteRatio( 'RATIO fast stack vs. heap',
- stackFastCreateTime, heapCreateTime );
- WriteRatio( 'RATIO fast stack vs. direct int assignment',
- stackFastCreateTime, intCreateTime );
-
- WriteLn( '-------------- DESTRUCTION' );
- WriteTime( 'Destruction in stack', stackDestroyTime );
- WriteTime( 'Fast destruction in stack', stackFastDestroyTime );
- WriteTime( 'Destruction in heap', heapDestroyTime );
- WriteRatio( 'RATIO stack vs. heap',
- stackDestroyTime, heapDestroyTime );
- WriteRatio( 'RATIO fast stack vs. heap',
- stackFastDestroyTime, heapDestroyTime );
- WriteLn( '-------------- OVERALL RESULTS' );
- WriteTime( 'Fast Stack creation + dest.', stackFastCreateTime + stackFastDestroyTime );
- WriteTime( 'Stack creation + destruction', stackCreateTime + stackDestroyTime );
- WriteTime( 'Heap creation + destruction', heapCreateTime + heapDestroyTime );
- WriteRatio( 'RATIO stack vs. heap', stackCreateTime + stackDestroyTime,
- heapCreateTime + heapDestroyTime );
- WriteRatio( 'RATIO fast stack vs. heap', stackFastCreateTime + stackFastDestroyTime,
- heapCreateTime + heapDestroyTime );
- WriteRatio( 'RATIO fast stack vs. direct int assignment',
- stackFastCreateTime + stackFastDestroyTime, intCreateTime + intDestroyTime );
- WriteLn( '------------------------------------------------------------------------------' );
- WriteLn;
- end;
-
- procedure TFMain.WriteRatio( const msg : string; time1, time2 : TTime );
- var
- ratio : TTime;
- begin
- ratio := time2 / time1;
- if ratio >= 1.0 then
- WriteLn( Format( '%s: %n times faster', [msg, ratio] ) )
- else begin
- ratio := time1 / time2;
- WriteLn( Format( '%s: %n times slower', [msg, ratio] ) )
- end;
- end;
-
- procedure TFMain.WriteTime(const msg: string; time: TTime);
- var
- opsPerSecond : TTime;
- microSecs : TTime;
- cnt : TTime;
- begin
- opsPerSecond := COUNT*1.0/time;
- cnt := COUNT;
- microsecs := time*1000000.0;
- WriteLn( Format( '%-30s %20.3n %10.0n %16n',
- [msg, microsecs, cnt, opsPerSecond] ) );
- end;
-
- procedure TFMain.WriteHeader;
- begin
- WriteLn( Format( 'Timer resolution: %10.6n microseconds', [TPSTimer.Resolution*1000000.0] ) );
- WriteLn;
- WriteLn( Format('%-30s %20s %10s %16s', ['Operation', 'Time (microsecs.)', 'Ops.', 'Ops./second' ]) );
- WriteLn( '------------------------------------------------------------------------------' );
- end;
-
- procedure TFMain.WriteLn(const msg: string);
- begin
- meOutput.Lines.Add( msg );
- end;
-
- procedure TFMain.btClearClick(Sender: TObject);
- begin
- meOutput.Lines.Clear;
- end;
-
- procedure TFMain.btTraceClick(Sender: TObject);
- begin
- WriteLn( '--------- Successful construction' );
- Trace( rpNone );
-
- WriteLn( '--------- Construction failed due to a NewInstance failure' );
- Trace( rpNewInstance );
-
- WriteLn( '--------- Construction failed due to a Create failure' );
- Trace( rpCreate );
-
- WriteLn( '--------- Construction failed due to a base class Create failure' );
- Trace( rpBaseCreate );
-
- WriteLn( '--------- Construction failed due to an AfterConstruction failure' );
- trace( rpAfterConstruction );
-
- WriteLn( '--------- Construction failed due to a base class AfterConstruction failure' );
- Trace( rpBaseAfterConstruction );
- end;
-
- end.
-