Einzelnen Beitrag anzeigen

Go2EITS

Registriert seit: 25. Jun 2006
519 Beiträge
 
Delphi 7 Personal
 
#1

Funktionsaufruf: Dauert immer länger. Warum?

  Alt 11. Apr 2009, 08:05
Hallo, nachstehendes Programm bitte einmal ausprobieren.

In der vorliegenden Version wird Clearstring1 immer langsamer. Ich kann es nicht erklären.
Die drei Funktionen unterscheiden sich nur durch die Konstanten:
1. ValidChars = ['a'..'z','A'..'Z'];
2. ValidChars = ['a'..'z','A'..'Z',' '];
3. ValidChars = ['a'..'z','A'..'Z','ä','ö','ü','Ä','Ö','Ü','ß',' '];

Paradoxerweise ist die zweite Variante schneller als die erste! (Ich habe nur ein SPACE in ValidChars hinzugefügt). Probiert dies bitte aus.

Zur Funktion: Clearstring gibt einen "bereinigten" String zurück.
Welche Zeichen dann enthalten sind, wird mit z.B. ValidChars = ['a'..'z','A'..'Z']; definiert.
Delphi-Quellcode:
program ClearString;

{$APPTYPE CONSOLE}

uses
  //FastMM4,FastMove,
  SysUtils, // Für Profiler.inc notwendig
  Windows; // Für Profiler.inc notwendig
{$include Profiler.inc}
var
  xx,ii:Integer;
// Langsame Variante
function ClearStr1(const Str: string): String;
var
  i:Integer;
const
ValidChars = ['a'..'z','A'..'Z'];
  begin
  if Str = 'then Exit;
  for i:=1 to Length(Str) do if str[i] in ValidChars then result:=result+Str[i];
end;

// Diese ist schneller...
function ClearStr2(const Str: string): String;
var
  i:Integer;
const
ValidChars = ['a'..'z','A'..'Z',' ']; // Schnelle Variante
  begin
  if Str = 'then Exit;
  for i:=1 to Length(Str) do if str[i] in ValidChars then result:=result+Str[i];
end;

function ClearStr3(const Str: string): String;
var
  i:Integer;
const
ValidChars = ['a'..'z','A'..'Z','ä','ö','ü','Ä','Ö','Ü','ß',' '];
  begin
  if Str = 'then Exit;
  for i:=1 to Length(Str) do if str[i] in ValidChars then result:=result+Str[i];
end;


begin
Writeln('Teste... ClearStr1');
for xx:=1 to 10 do
   begin
   Start;
   for ii:=1 to 10000 do ClearStr1('>a-uf d?er Ma!uer<');
   Stopp;
   end;
Writeln('Ergebnis muss - aufderMauer - sein:');
Writeln(ClearStr1('>a-uf d?er Ma!uer<'));Writeln;


Writeln('Teste... ClearStr2');
for xx:=1 to 10 do
    begin
    Start;
    for ii:=1 to 10000 do ClearStr2('>a-uf d?er Ma!uer<');
    Stopp;
    end;
Writeln('Ergebnis muss - auf der Mauer - sein:');
Writeln(ClearStr2('>a-uf d?er Ma!uer<'));Writeln;

Writeln('Teste... ClearStr3');
for xx:=1 to 10 do
    begin
    Start;
    for ii:=1 to 10000 do ClearStr3('>a-uf d?er Ma!uer<');
    Stopp;
    end;
Writeln('Ergebnis muss - auf der Mauer - sein:');
Writeln(ClearStr3('>a-uf d?er Ma!uer<'));Writeln;

Writeln('- ProgrammEnde mit [ENTER] -');Readln;

end.
Die Include:
Delphi-Quellcode:
// Date: 08.04.2009
// File: Profiler.inc

//Timer Variablen
 var Profiler_StartWert, Profiler_StopWert, Profiler_Freq: int64;

/// Profiler Routinen Start...
procedure Start;
begin
QueryPerformanceFrequency(Profiler_Freq);
QueryPerformanceCounter(Profiler_StartWert);
end;

procedure Stopp;
begin
QueryPerformanceCounter(Profiler_StopWert);
Writeln('Profiler: '+floattostr(((Profiler_StopWert-Profiler_StartWert)/Profiler_Freq)*1000)+' ms');
end;
/// ...Profiler Routinen Ende
Ich habe, eigentlich nur aus "Spaß", die FastMM4 mit eingebunden, da funktioniert es plötzlich.
Ist es ein BUG in Delphi 7? Und wenn ja, kann ich diesen ohne FastMM4 umgehen?
[Edit: Titel geändert]
  Mit Zitat antworten Zitat