Pastebin
Paste #2418: readfile.lpr
< previous paste - next paste>
Pasted by tdn
program readfile; {$mode objfpc}{$H+} uses {$IFDEF UNIX}{$IFDEF UseCThreads} cthreads, {$ENDIF}{$ENDIF} Classes, SysUtils, CustApp, windows { you can add units after this }; type { readfile } rfile = class(TCustomApplication) protected procedure DoRun; override; public constructor Create(TheOwner: TComponent); override; destructor Destroy; override; procedure WriteHelp; virtual; end; { rfile } procedure rfile.DoRun; var ErrorMsg, FileName: String; Fin : File; NumRead, PrevNumRead : Word; //Buf : Array[1..512] of byte; //Buf : Array[1..1024] of byte; //Buf : Array[1..2048] of byte; //Buf : Array[1..4096] of byte; //Buf : Array[1..8128] of byte; //Buf : Array[1..16256] of byte; Buf : Array of Byte; BufSize, NumReadOps, StartTime, EndTime, ExecTime, Size: Int64; Speed : Double; begin // quick check parameters ErrorMsg:=CheckOptions('h','help'); if ErrorMsg<>'' then begin ShowException(Exception.Create(ErrorMsg)); Terminate; Exit; end; // parse parameters if HasOption('h','help') then begin WriteHelp; Terminate; Exit; end; if HasOption('bufsize') then begin BufSize:=2048; Terminate; Exit; end; If Paramcount < 1 then WriteHelp; // Initialize variables NumRead := 0; FileName := ParamStr(1); BufSize := 2048; SetLength(Buf, BufSize); Bufsize := Length(Buf); WriteLn(); WriteLn(BufSize); NumReadOps:=0; // Number of read operations If FileExists (FileName) then begin System.Assign (Fin, FileName); Reset (Fin,1); WriteLn('Reading "',FileName,'"'); WriteLn('FileSize: ',FileSize(Fin), ' bytes'); WriteLn('Bufsize:', bufsize); StartTime := Windows.GetTickCount(); Repeat PrevNumRead := NumRead; BlockRead (Fin,buf,BufSize,NumRead); inc(NumReadOps); //If NumReadOps mod 100000 = 0 then // writeln(Total, ' : ', NumReadOps * BufSize); Until (NumRead=0); close(Fin); EndTime := Windows.GetTickCount(); // Add 1 in order to avoid divbyzero on very short exectime ExecTime:= EndTime - StartTime + 1; WriteLn(); //WriteLn('Numread=',NumRead); //WriteLn('PrevNumRead=',PrevNumRead); //writeln(NumReadOps, ' : ', NumReadOps * BufSize); Size := (NumReadOps-2) * BufSize + PrevNumRead; If Size < 0 then Size := 0; Speed := Size / ExecTime; WriteLn ('Read ',Size, ' bytes (', FloatToStrF(Size/1024/1024, ffFixed,1,1), ' mb) from file ',paramstr(1), ' in ', ExecTime, ' ms :'); Speed := Size / (ExecTime/1000); WriteLn(FloatToStrF(speed,ffFixed,1,2), ' b/s'); WriteLn(FloatToStrF(speed/1024,ffFixed,1,2), ' kb/s'); WriteLn(FloatToStrF(speed/1024/1024,ffFixed,1,2), ' mb/s'); WriteLn(FloatToStrF(speed/1024/1024/1024,ffFixed,1,2), ' gb/s'); end else begin Writeln ('File "'+Paramstr(1)+'" NOT found'); end; //Write('Enter to quit'); //ReadLn; // stop program loop Terminate; end; constructor rfile.Create(TheOwner: TComponent); begin inherited Create(TheOwner); StopOnException:=True; end; destructor rfile.Destroy; begin inherited Destroy; end; procedure rfile.WriteHelp; begin { add your help code here } // writeln('Usage: ',ExeName,' -h'); WriteLn('Usage: '+ParamStr(0)+' <filename>'); Halt; end; var Application: rfile; {$R *.res} begin Application:=rfile.Create(nil); Application.Title:='rfile'; Application.Run; Application.Free; end.
New Paste
Go to most recent paste.