AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Code Optimization

Ein Thema von sdean · begonnen am 9. Mai 2011 · letzter Beitrag vom 10. Mai 2011
Antwort Antwort
Seite 1 von 2  1 2      
sdean

Registriert seit: 5. Dez 2009
64 Beiträge
 
#1

Code Optimization

  Alt 9. Mai 2011, 17:31
Delphi-Version: 7
Hi i've these functions to Store Streams in One File :
Delphi-Quellcode:
function AddData(ID:string; pBuf: Pointer; Count: Integer): Integer;
var
  Buff : Pointer;
begin
 // FFile is of Type : TFileStream
  FFile.Seek(0, soFromEnd);//--> the Container *
  if( Result > -1 )then
  begin
   GetMem(Buff, Count);
   try
    Move(pBuf^, Buff^, Count);
    Result := FFile.Write(Buff^, Count);
   finally
    FreeMem(Buff, Count);
   end;
  end;
end;
{* and here how i add Streams *}
function InsertStream(ID: String;aStream: TStream): Integer;
var
  pBuff : Pointer;
begin
  // alloc Mem for pBuff
  GetMem(pBuff, aStream.Size);
  try
   aStream.Position := 0;
   aStream.Read(pBuff^, aStream.Size);
   // now add data
   Result := AddData(ID, pBuff, aStream.Size);
  finally
   FreeMem(pBuff, aStream.Size);
  end;
end;
my question is :
how could i optimize the InsertStream Function so that it can work fast for file's size more than 1 GB cause i cannot load such a huge size in Memory.


many thanks in advance
  Mit Zitat antworten Zitat
FredlFesl

Registriert seit: 19. Apr 2011
293 Beiträge
 
Delphi 2009 Enterprise
 
#2

AW: Code Optimization

  Alt 9. Mai 2011, 19:48
The Code contains errors and redundant stuff.
Here's my improved / modified and untested version.
Delphi-Quellcode:
Const
  BLOCKSIZE = 1024*1024; // Write in chunks @ 1MB

function AddData(ID:string; pBuf: Pointer; Count: Integer): Integer;
begin
  FFile.Seek(0, soFromEnd);
  if( Result > -1 )then // ***** WHERE DOES THIS Result COME FROM? if you leave it Like this, it will not work.
    Result := FFile.Write(PAnsiChar(pBuf)^, Count);
end;

function InsertStream(ID: String;aStream: TStream): Integer;
var
  pBuff : Pointer;
  BytesRead : Integer;

begin
  GetMem(pBuff, BLOCKSIZE); // allocate a chunk of memory
  try
   aStream.Position := 0;
   Repeat
     BytesRead := aStream.Read(pBuff^, MIN (BLOCKSIZE, aStream.Size - aPosition)); // move data into the chunk
     Result := AddData(ID, pBuff, BytesRead); // Add the chunk to the file
   Until (Result<>0) or (BytesRead < BLOCKSIZE);
  finally
   FreeMem(pBuff, BLOCKSIZE);
  end;
end;
However, a much simpler approach would be
Delphi-Quellcode:
FFile.Seek(0, soFromEnd);
FFile.CopyFrom (aStream, FFile.Position);
Das Bild hängt schief.
  Mit Zitat antworten Zitat
sdean

Registriert seit: 5. Dez 2009
64 Beiträge
 
#3

AW: Code Optimization

  Alt 9. Mai 2011, 20:41
thank you FredlFesl

BytesRead := aStream.Read(pBuff^, MIN (BLOCKSIZE, aStream.Size - aPosition)); aPosition >> do you mean aStream.Position
  Mit Zitat antworten Zitat
Benutzerbild von Luckie
Luckie

Registriert seit: 29. Mai 2002
37.621 Beiträge
 
Delphi 2006 Professional
 
#4

AW: Code Optimization

  Alt 9. Mai 2011, 20:46
Why is FFile global? If it is global because it is needed in different parts of the code, why don't you make a class out of the code?
Michael
Ein Teil meines Codes würde euch verunsichern.
  Mit Zitat antworten Zitat
sdean

Registriert seit: 5. Dez 2009
64 Beiträge
 
#5

AW: Code Optimization

  Alt 9. Mai 2011, 20:50
thank you Luckie :
any help of that !
  Mit Zitat antworten Zitat
FredlFesl

Registriert seit: 19. Apr 2011
293 Beiträge
 
Delphi 2009 Enterprise
 
#6

AW: Code Optimization

  Alt 9. Mai 2011, 20:53
thank you FredlFesl

BytesRead := aStream.Read(pBuff^, MIN (BLOCKSIZE, aStream.Size - aPosition)); aPosition >> do you mean aStream.Position
Yes.
Das Bild hängt schief.
  Mit Zitat antworten Zitat
Benutzerbild von Luckie
Luckie

Registriert seit: 29. Mai 2002
37.621 Beiträge
 
Delphi 2006 Professional
 
#7

AW: Code Optimization

  Alt 9. Mai 2011, 21:00
The class could look like this:
Delphi-Quellcode:
TXyz = class
private
  FFile: TFileStream;
public
  property XyzFile: TFileStream read FFile write FFile;
  procedure AddData(...);
  procedure InsertData(...);
end;
Michael
Ein Teil meines Codes würde euch verunsichern.
  Mit Zitat antworten Zitat
sdean

Registriert seit: 5. Dez 2009
64 Beiträge
 
#8

AW: Code Optimization

  Alt 9. Mai 2011, 21:01
but FredlFesl : your improved / modified and untested version writes Only 1MB to FFile
i mean the FFile will only hold 1MB of the Source Stream not the Whole Stream size .
  Mit Zitat antworten Zitat
sdean

Registriert seit: 5. Dez 2009
64 Beiträge
 
#9

AW: Code Optimization

  Alt 9. Mai 2011, 21:08
The class could look like this:
Delphi-Quellcode:
TXyz = class
private
  FFile: TFileStream;
public
  property XyzFile: TFileStream read FFile write FFile;
  procedure AddData(...);
  procedure InsertData(...);
end;
Thank you Luckie , Could you please explain :
what s the Diff between Declaring FFile as global and Creating this New Class as long as the Result will remain the same : the Write / Read Operations will be focusing on the FFile .

and many thanks .
  Mit Zitat antworten Zitat
Benutzerbild von Luckie
Luckie

Registriert seit: 29. Mai 2002
37.621 Beiträge
 
Delphi 2006 Professional
 
#10

AW: Code Optimization

  Alt 9. Mai 2011, 21:16
The result will of course be the same. But encapsulating the code makes it more readable, easier to administrate and (re)use.
Michael
Ein Teil meines Codes würde euch verunsichern.
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 1 von 2  1 2      


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 23:35 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz