Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Format-Funktion zu langsam (https://www.delphipraxis.net/38859-format-funktion-zu-langsam.html)

Jelly 25. Jan 2005 11:14


Format-Funktion zu langsam
 
Für eine simulation muss ich eine 200x200x200=800.000 Werte in einer Textfile speichern. Dazu ruf ich 800.000 mal die Format-Funktion auf:
Delphi-Quellcode:
format('%.4f ',[C[Nr]]) ;
Das dauert aber ELEND lange... Gibts da nicht eine Methode, die da wesentlich schneller arbeitet... FloatToStr bringt mir auch nix.

Steve 25. Jan 2005 11:18

Re: Format-Funktion zu langsam
 
Hi,

evtl. ist Str() schneller..?

Gruß
Stephan :dance:

sakura 25. Jan 2005 11:26

Re: Format-Funktion zu langsam
 
Zitat:

Zitat von Jelly
200x200x200=800.000

Das mit dem Diplom (oder Doktor :gruebel:) wird nichts! 200^3 = 8.000.000 (8 Millionen!) :mrgreen:

Unabhängig davon, sind ca. 13 Sekunden für 8Mio Werte wirklich zu viel?

Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
var
  Start, Stop, Freq: Int64;
  I: Integer;
  Id: Double;
begin
  QueryPerformanceCounter(Start);
  for I := 1 to 8000000 do
  begin
    Id := I;
    Format('%.4f', [Id]);
  end;
  QueryPerformanceCounter(Stop);
  QueryPerformanceFrequency(Freq);
  Label1.Caption := Format('%f ms', [(Stop - Start) * 1000 / Freq]);
end;
...:cat:...

shmia 25. Jan 2005 12:55

Re: Format-Funktion zu langsam
 
Zitat:

Zitat von Jelly
Gibts da nicht eine Methode, die da wesentlich schneller arbeitet... FloatToStr bringt mir auch nix.

FormatFloat sollte deutlich schneller sein, da dass Parsen des Formatstrings entfällt.
Daneben gibt es noch: FloatToStrF und FloatToDecimal & FloatToText

Jelly 25. Jan 2005 12:59

Re: Format-Funktion zu langsam
 
Zitat:

Zitat von sakura
Zitat:

Zitat von Jelly
200x200x200=800.000

Das mit dem Diplom (oder Doktor :gruebel:) wird nichts! 200^3 = 8.000.000 (8 Millionen!) :mrgreen:

:wall: Dann verlängert sich die Rechenzeit ja nochmals um den Faktor 10 :mrgreen:

Zitat:

Zitat von sakura
Unabhängig davon, sind ca. 13 Sekunden für 8Mio Werte wirklich zu viel?

Das ist echt seltsam? Bei mir dauerts über ne halbe Stunde. Lass ich die format Funktion weg, und schreibe direkt immer 1.0000 als Wert, dauerts paar Sekunden :gruebel: Also da bin ich jetzt etwas überfragt.

Luckie 25. Jan 2005 14:10

Re: Format-Funktion zu langsam
 
Zeig mal den Code drumherum.

Sharky 25. Jan 2005 15:12

Re: Format-Funktion zu langsam
 
Zitat:

Zitat von Luckie
Zeig mal den Code drumherum.

Ich habe das eben mal mit diesem Code gemacht:
Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
var
  Start, Stop, Freq: Int64;
  ndx : Integer;
  sl : TStringList;
  Id: Double;
begin
  QueryPerformanceCounter(Start);
  sl := TStringList.Create;
  try
    for ndx := 1 to 8000000 do
    begin
      Id := ndx;
//      sl.Add (FloatToStr (id)); // Variante 1
//      sl.Add(Format('%.4f', [Id])); // Variante 2
      sl.Add(FormatFloat('########.0000',ID)); // Variante 3
    end;
    QueryPerformanceCounter(Stop);
    QueryPerformanceFrequency(Freq);
    Label1.Caption := Format('%f ms', [(Stop - Start) * 1000 / Freq]);
    sl.SaveToFile('c:\liste3.txt');
  finally
   sl.Free;
  end;
end;
Variante 1 : 5.671 ms
Variante 2 : 7.143 ms
Varinate 3 : 7.657 ms

Jelly 25. Jan 2005 15:20

Re: Format-Funktion zu langsam
 
Zitat:

Zitat von Luckie
Zeig mal den Code drumherum.

Delphi-Quellcode:
    { 3D-Matrix mit Parametern }
    for Nr := 1 to 2 do begin
        for iz := 0 to G.z-1 do begin
           V_line[Nr] := format('%s (:,:,%d) = [',[A[Nr],iz+1]) ;
           for ix := 0 to G.x-1 do begin
              for iy := 0 to G.y-1 do with Thread.Cells[ix,iy,iz] do begin
                 V_line[Nr] := V_line[Nr] + FloatToStrF (C[Nr],ffFixed,7,4) + ' ' ; // langsam, heisst ung. 20 min.
                 //V_line[Nr] := V_line[Nr] + '0.0001 ' ; // schnell, par Sekündchen
                 if (iy = G.y-1) and (ix < G.x-1) then begin
                    V_line[Nr] := V_line[Nr] + '; ' ;
                 end ;
              end ;
           end ;
           V_line[Nr] := V_line[Nr] + '] ;' ;
           Add (V_line[Nr]) ;
           V_line[Nr] := '' ;
        end ;
        Add ('') ;
    end ;
Die Add Methode... FS ist ein TFileStream:

Delphi-Quellcode:
procedure TMCBasisExport.Add(AText: string);
var
 buf : string ;
begin
     if FS <> nil then begin
         buf := AText+#13#10 ;
         FS.Write(buf[1],length(buf)) ;
     end else begin
         raise exception.Create('Datei kann nich beschrieben werden.');
     end ;end;
An der Add Methode kanns aber meiner Ansicht nach nicht liegen, da die ja auch aufgerufen wird, wenn ich Format weglasse, und direkt einen Wert schreibe.

shmia 26. Jan 2005 11:02

Re: Format-Funktion zu langsam
 
[quote="Jelly"]
Delphi-Quellcode:
    { 3D-Matrix mit Parametern }
    for Nr := 1 to 2 do begin
        for iz := 0 to G.z-1 do begin
           V_line[Nr] := format('%s (:,:,%d) = [',[A[Nr],iz+1]) ;
           for ix := 0 to G.x-1 do begin
              for iy := 0 to G.y-1 do with Thread.Cells[ix,iy,iz] do begin
                 V_line[Nr] := V_line[Nr] + FloatToStrF (C[Nr],ffFixed,7,4) + ' ' ; // langsam, heisst ung. 20 min.
                 //V_line[Nr] := V_line[Nr] + '0.0001 ' ; // schnell, par Sekündchen
Deine Auskommentierung ist nicht fair, da du den Zugriff auf das Array C[] nicht berücksichtigt:
Delphi-Quellcode:
// Variante 1
V_line[Nr] := V_line[Nr] + FloatToStrF (C[Nr],ffFixed,7,4) + ' ' ;

// Variante 2 (ohne FloatToStrF)
dummy_float := C[Nr]; // optimierung des compilers ausschalten, sonst wird wegoptimiert
// und da da Array C[] wohl zu Thread.cells gehört, wird dies wohl die Hauptursache
// für dein Performanceproblem sein !!! 
V_line[Nr] := V_line[Nr] + '0.0001 ' ;

Jelly 26. Jan 2005 12:52

Re: Format-Funktion zu langsam
 
@shmia: hättest du Recht haben können... Habs aber grad getestet, und das ändert aber nichts am Verlauf :gruebel:


Alle Zeitangaben in WEZ +1. Es ist jetzt 08:45 Uhr.
Seite 1 von 2  1 2      

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