Autor Beitrag
Tigerandy
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 21



BeitragVerfasst: Di 23.08.05 13:58 
Hallo,

wie lässt sich das am komfortabelsten lösen?

Gruß
Tiger
retnyg
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2754

SNES, GB, GBA, CPC, A500, 486/66, P4/3.0HT: NintendOS, AmigaOS, DoS
Delphi 5, Delphi 7
BeitragVerfasst: Di 23.08.05 14:06 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
function cutlastchar(s:string):string;
var l: integer;
begin
    l := length(s);
    if l > 0 then begin
      dec( l);
      setlength(result, l);
      while l > 0 do begin
        result[l] := s[l];
        dec(l);
      end;
    end;
end;

_________________
es gibt leute, die sind genetisch nicht zum programmieren geschaffen.
in der regel haben diese leute die regel...
F34r0fTh3D4rk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5285
Erhaltene Danke: 29

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Di 23.08.05 14:08 
was ist mit dem hier:
ausblenden Delphi-Quelltext
1:
  delete(str, length(str), 1);					


ist auf jedenfall komfortabler ;)
WeBsPaCe
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 2322
Erhaltene Danke: 1

FireFox 3, Internet Explorer 6 SP1
D1, D3Prof, D6Pers, D7Pers+Indy, VisualStudio Express
BeitragVerfasst: Di 23.08.05 14:09 
Also ich bin ja normalerweise überhaupt kein Freund von copy aber hier ist's dann doch wohl am einfachsten... :lol:

ausblenden Delphi-Quelltext
1:
2:
3:
function CutLastChar(AString: String): String;
  Result := Copy(AString, 0, Length(AString) - 1);
end;



slt, WeBBy

//EDIT: Nicht dich gemeint, Seth. ;) Da war nur rety's Post da. ;)
F34r0fTh3D4rk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5285
Erhaltene Danke: 29

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Di 23.08.05 14:11 
ich denke meins ist kürzer 8)
das kann man ja noch in ne prozedur oder funktion packen und besser kann mans net haben, dann ist der inhalt aber wieder egal und die frage: "was ist am schnellsten?"

:D

EDIT: *g* :wink:
WeBsPaCe
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 2322
Erhaltene Danke: 1

FireFox 3, Internet Explorer 6 SP1
D1, D3Prof, D6Pers, D7Pers+Indy, VisualStudio Express
BeitragVerfasst: Di 23.08.05 14:15 
Ruft Delete vielleicht intern nur Copy auf? Dann ist mein's schneller... :tongue:
F34r0fTh3D4rk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5285
Erhaltene Danke: 29

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Di 23.08.05 14:17 
wir können ja mal zeitmessungen durchführen und versuchen das weitestgehend zu optimieren, vielleicht geht's ja noch simpler 8)

ausblenden Delphi-Quelltext
1:
  setlength(str, length(str) - 1);					
FinalFantasy
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 127

Windows XP
Delphi 5 Professional, Visual Studio 7 .NET (C#)
BeitragVerfasst: Di 23.08.05 14:19 
wie wärs mit String[length(string)] := #0??
Da braucht man gar nichts kopieren *gg*
F34r0fTh3D4rk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5285
Erhaltene Danke: 29

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Di 23.08.05 14:22 
auch ne methode :D
ausblenden Delphi-Quelltext
1:
setlength(str, length(str) - 1)					

braucht für 1 mio zeichen 78ms


Zuletzt bearbeitet von F34r0fTh3D4rk am Di 23.08.05 14:26, insgesamt 1-mal bearbeitet
WeBsPaCe
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 2322
Erhaltene Danke: 1

FireFox 3, Internet Explorer 6 SP1
D1, D3Prof, D6Pers, D7Pers+Indy, VisualStudio Express
BeitragVerfasst: Di 23.08.05 14:25 
user profile iconFinalFantasy hat folgendes geschrieben:
wie wärs mit String[length(string)] := #0??
Da braucht man gar nichts kopieren *gg*

:rofl: Ja, so kann man's auch machen... :lol: :mrgreen:
F34r0fTh3D4rk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5285
Erhaltene Danke: 29

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Di 23.08.05 14:28 
ausblenden Delphi-Quelltext
1:
Str[i] := #0;					

braucht für 5 mio zeichen nur 31ms

also 5 mal so viel in nichtmal der halben zeit 8)
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Di 23.08.05 14:28 
Moin!

Nein, kann man nicht, das sind Pascal-Strings! :mahn:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
procedure TForm1.Button1Click(Sender: TObject);
var
  test: string;
begin
  test := 'hallo';
  ShowMessage(IntToStr(Length(test)));
  test[Length(test)] := #0;
  ShowMessage(IntToStr(Length(test)));
end;

cu
Narses


Zuletzt bearbeitet von Narses am Di 23.08.05 14:30, insgesamt 1-mal bearbeitet
I.MacLeod
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 109



BeitragVerfasst: Di 23.08.05 14:29 
Erstmal, delete dürfte etwas schneller sein, da copy imho noch eine Kopie des Strings anfertigen muss, bei delete bin ich mir aber nicht so sicher. Ich glaube ich hab auch in der DOH mal was dazu gelesen. String[length(string)] := #0 ist zwar schnell, aktualisiert aber den Längencounter des Strings nicht. Nach Delphi wäre dann zum Beispiel ein "Hallo!", von dem mit dieser Methode das Ausrufezeichen abgeschnitten wurde != "Hallo". Wie es mit SetLength aussieht weiß ich auch nicht.

Cheers

_________________
{$APPTYPE CONSOLE}uses SysUtils;const a='{$APPTYPE CONSOLE}uses SysUtils;const a=%s;begin write(Format(a,[#39+a+#39]))end.';begin write(Format(a,[#39+a+#39]))end.
F34r0fTh3D4rk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5285
Erhaltene Danke: 29

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Di 23.08.05 14:30 
ABER: die länge des strings bleibt gleich :(
FinalFantasy
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 127

Windows XP
Delphi 5 Professional, Visual Studio 7 .NET (C#)
BeitragVerfasst: Di 23.08.05 14:30 
Ich denke der Aufruf von length() wird wesentlich aufwendiger sein, als die Zuweisung von #0.
Stimmt, die Länge müsste gleich bleiben, dafür dürfte es so ziemlich das schnellste sein.
Das übliche Problem: Speicherbedarf VS. Rechenleistung... beides gleichzeitig geht seltenst.


Zuletzt bearbeitet von FinalFantasy am Di 23.08.05 14:33, insgesamt 1-mal bearbeitet
F34r0fTh3D4rk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5285
Erhaltene Danke: 29

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Di 23.08.05 14:32 
man kann aber vorher in pchar umwandeln, das ergibt nichtmal 1ms zeitverlust, also immer noch am besten, copy und delete brauchen wir glaube ich net testen, die sind bestimmt es langsamer 8)

komischerweise funzt das als einzelne funktion nicht :?
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
function CutLastChar(str: string): string;
var
  pch: pchar;
begin
  pch := pchar(str);
  pch[length(pch)] := #0;
  result := string(pch);
end;


Zuletzt bearbeitet von F34r0fTh3D4rk am Di 23.08.05 14:37, insgesamt 1-mal bearbeitet
I.MacLeod
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 109



BeitragVerfasst: Di 23.08.05 14:34 
Umwandlung in PChar geht theoretisch in 0 Zeit, für diese Operation wird allerdings eine komplette Kopie des Strings notwendig sein, und anschliessend muss das Ganze wieder in einen String umgewandelt werden. Mmn wird delete schneller sein. Ich weiß nicht wie der MemoryManager das behandelt, aber im Idealfall wird nur eine Aktualisierung des Längenzählers und eine Zeichenzuweisung notwendig sein.

Cheers

Groß- & Kleinschreibung berichtigt

_________________
{$APPTYPE CONSOLE}uses SysUtils;const a='{$APPTYPE CONSOLE}uses SysUtils;const a=%s;begin write(Format(a,[#39+a+#39]))end.';begin write(Format(a,[#39+a+#39]))end.