visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Languages :: Delphi :: Knowledge Base : fastest truncation method for a file of records ? Delphi or Pascal


By: VGR France  Date: 21/03/2005 08:55:25  English  Points: 0 Status: Answered
Quality : Excellent
In here I compare the two first ideas that came to my mind : either (stupidly) BlockRead()ing the file, then BlockWrite()-ing it truncated, or use the Truncate() method after a Seek() if the file permits this.

Let's see.... Here's the code, and results are hereafter.

program texttruncate; {$APPTYPE CONSOLE} Uses temps_reel, Windows in 'H:\Borland\Delphi4\Source\Rtl\Win\windows.pas'; Type Testrecord = Record element1 : Integer; element2 : String[10]; End; // record Var thefile : File Of Testrecord; therec : Testrecord; i : LongWord; // Blockread data structures SecondFile : File Of Testrecord; NumRead, NumWritten : Integer; Buffer : array[1..16777216] of Char; Const cNbRec = 1048576; //1048576; // 262143; begin // write test file CountOn; Assign(thefile,'thefile1.dat'); ReWrite(thefile); for i:=0 to cNbRec Do Begin With therec Do Begin element1:=i; element2:='pipo'; End; Write(thefile,therec); End; // for Close(thefile); CountOff; WriteLn('Test File of ',cNbRec,' records (',cNbRec*SizeOf(Testrecord),' bytes) written in ',TimeCount,' seconds.'); // copy into file2 for security because of truncate CopyFile('thefile1.dat','thefile2.dat',False); // direct Win32 API call // truncate CountOn; Assign(SecondFile,'thefile2.dat'); ReSet(SecondFile); Seek(SecondFile,Filesize(SecondFile)-1); Truncate(SecondFile); Close(SecondFile); CountOff; WriteLn('Truncate in ',TimeCount,' seconds.'); // blockread CountOn; Assign(thefile,'thefile1.dat'); ReSet(thefile); Assign(SecondFile,'result1.dat'); ReWrite(SecondFile); (* Seek(thefile,FileSize(thefile)-1); // in records, thus *SizeOf(Testrecord) is useless i:=FilePos(thefile); // in bytes *) // BlockRead(thefile, Buffer, FileSize(thefile)-2, NumRead); // -2 because first element is #0 BlockWrite(SecondFile, Buffer, NumRead, NumWritten); Close(SecondFile); CountOff; WriteLn('BlockWritten in ',TimeCount,' seconds.'); // wait for user ReadLn; end.

By: VGR Date: 21/03/2005 08:56:32 English  Type : Answer
Results are :

Test File of 1048576 records (16777216 bytes) written in 1.16170000000000E+0001 seconds. Truncate in 0.00000000000000E+0000 seconds. BlockWritten in 1.85300000000000E+0000 seconds.



So the 16 MB test file was written in 11,6 seconds, truncated in 0 s (!) and truncated using Block operations in 1,85 s

Do register to be able to answer

EContact
browser fav
page generated in 648.309950 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page