Autor Beitrag
morphi
Hält's aus hier
Beiträge: 3



BeitragVerfasst: Fr 25.02.05 17:51 
Ich möchte den Text aus der Textbox Edit2 in Edit1 umgedreht haben also das aus Wurzel "lezruW" wird. z.B. ich bin anfäger

danke Morphi
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Fr 25.02.05 17:57 
:welcome:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
Var
 A, B : String;
 I : Integer;
begin
 A := 'Wurzel';
 B := A;
 For I := 1 to length(A) do
  B[length(A)-I+1] := A[I];
 // B := 'lezruW';
end;
uall@ogc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1826
Erhaltene Danke: 11

Win 2000 & VMware
Delphi 3 Prof, Delphi 7 Prof
BeitragVerfasst: Fr 25.02.05 18:03 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
varr   
 A : String;   
 I : Integer; 
 B : char;   
begin   
 A := 'Wurzel';   
 For I := 1 to length(A) do 
 begin
  b := a[i];
  A[I] := A[length(A)-i+1]; 
  A[length(A)-i+1] := b;
 end;  
end;


für lange strings ist das besser da kein 2ter string angelegt werden muss


Zuletzt bearbeitet von uall@ogc am Fr 25.02.05 18:13, insgesamt 1-mal bearbeitet
BeniSchindler
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 82

Win 2k / suse9.1
D7 Prof / Kylix 3
BeitragVerfasst: Fr 25.02.05 18:03 
Oder :

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
Var
 A, B : String;
 Index : Integer;
begin
 A := 'Wurzel';
 B := '';
 For Index :=  length(A) downto 1 do B := B + A[Index];
 // B := 'lezruW';
end;

_________________
Result := 42; Application.ProcessMessages;
uall@ogc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1826
Erhaltene Danke: 11

Win 2000 & VMware
Delphi 3 Prof, Delphi 7 Prof
BeitragVerfasst: Fr 25.02.05 18:12 
das ist bis jetzt die schlechteste variante da delphi da immer einen neuen string erstellen muss
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Fr 25.02.05 18:15 
So werden ständig neue Strings aneinandergehängt :O

//Edit: Ok stimmt glaub ich nicht ganz.. :\ auf jeden Fall ist es performancemässig am besten, wenn du nicht ständig was zu einem String dazufügst. Wenn die Länge eines Strings am Anfang schon gegeben ist, dann solltest du das auch am Anfang festlegen.


Zuletzt bearbeitet von delfiphan am Fr 25.02.05 18:19, insgesamt 1-mal bearbeitet
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6393
Erhaltene Danke: 147

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Fr 25.02.05 18:18 
Dürfte noch schneller sein:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
Var
 A, B : String;
 Index : Integer;
 laenge : Integer;
begin
 A := 'Wurzel';
 laenge := Length(A); // Damit Length nur einmal aufgerufen werden muss
 SetLength(B,laenge);
 For Index := laenge downto 1 do B[laenge+1-Index] := A[Index];
end;
uall@ogc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1826
Erhaltene Danke: 11

Win 2000 & VMware
Delphi 3 Prof, Delphi 7 Prof
BeitragVerfasst: Fr 25.02.05 18:20 
jasocul
1.) hast du 2 strings -> das mag ich schon net ;>
2.) downto schleife langsamer als for to schleife
3.) speichert delphi die länge eh in einem register (ebx) von daher isses egal ob du das vorher in eine variable auslagerst
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Fr 25.02.05 18:22 
Wer hat Lust, das ganze jetzt noch in Assembler zu schreiben :D
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Fr 25.02.05 18:26 
Ich denke sowieso nicht, dass length(S) langsamer ist als ne gewöhnliche Variable zu lesen. Length wird nicht in einen call verwandelt, oder? Zu mindest nicht bei Pascal-Strings.
uall@ogc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1826
Erhaltene Danke: 11

Win 2000 & VMware
Delphi 3 Prof, Delphi 7 Prof
BeitragVerfasst: Fr 25.02.05 18:27 
length(s) wird bei einer schleifenvariable in EBX geschrieben, sollte also genausoschnell / shcneller sein
da delphi auch die variable in EBX packen würde
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Fr 25.02.05 18:29 
Also ich meinte eher grundsätzlich so... Bzw. beispielsweise in der For-Schleife drin:
ausblenden Delphi-Quelltext
1:
2:
For I := 1 to length(A) do  
  B[length(A)-I+1] := A[I];
uall@ogc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1826
Erhaltene Danke: 11

Win 2000 & VMware
Delphi 3 Prof, Delphi 7 Prof
BeitragVerfasst: Fr 25.02.05 18:32 
meine variante hatte sogar noch nen bug

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
var s: string;
    i, l: integer;
    b: char;
begin
  s := 'das ist ein test';
  l := length(s);
  for i := 1 to l div 2 do
  begin
    b := s[i];
    s[i] := s[l-i+1];
    s[l-i+1] := b;
  end;
  form1.caption := s;
end;

ich muss nur die hälfte des strings machen ;>
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6393
Erhaltene Danke: 147

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Fr 25.02.05 18:34 
Neue Variante und Kommentare:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
Var
 A : String;
 B : Char;
 Index : Integer;
 Ende : Integer;
 laenge : Integer;
begin
 A := 'Wurzel';
 laenge := Length(A); // Damit Length nur einmal aufgerufen werden muss
 Ende := (laenge div 2)+1;
 For Index := laenge downto ende do begin
   B := A[laenge+1-Index];
   A[laenge+1-Index] := A[Index];
   A[Index] := B;
 end;
end;

Da ich die Länge mehrfach benötige macht es durchaus Sinn. Ich hätte allerdings auch auf A[0] zugreifen können. Da kann es aber zu Problemen mit AnsiStrings kommen.
Die neue Variante hat auch nur noch einen String.
Eine for..downto ist meistens schneller als for..to. Dazu gab es auch schon Threads, weil Delphi das auch in diese Richtung optimiert, wenn es möglich ist.
Die Schleife läuft jetz nur noch über die Hälfte des Strings.
Wenn jetzt noch was verbessert werden soll, läuft es auf Assembler raus.

//EDIT:
Mist. Hätte ich nicht kommentiert wäre ich schneller gewesen als uall@ogc :wink:
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Fr 25.02.05 18:39 
morphi hat folgendes geschrieben:
ich bin anfäger

Morphi wird Freude haben :)

Eben genau das war meine Frage: Ob denn length(S) in einen Function-Call oder bei normalen Strings direkt in ein S[0] übersetzt wird.

//Edit: Hui, ist aber schwach, es gibt tatsächlich einen Function Call. Hätte jetzt fast gedacht, dass das zu S[0] optimiert wird.


Zuletzt bearbeitet von delfiphan am Fr 25.02.05 18:43, insgesamt 2-mal bearbeitet
Sprint
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 849



BeitragVerfasst: Fr 25.02.05 18:41 
morphi hat folgendes geschrieben:
Ich möchte den Text aus der Textbox Edit2 in Edit1 umgedreht haben also das aus Wurzel "lezruW" wird. z.B. ich bin anfäger

ausblenden Delphi-Quelltext
1:
  Edit1.Text := AnsiReverseString(Edit2.Text);					

Unter Delphi 7 gibt es die Funktion AnsiReverseString. Unter Delphi 5 nicht. Bei Delphi 6 weiß ich es nicht.

_________________
Ciao, Sprint.
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6393
Erhaltene Danke: 147

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Fr 25.02.05 18:43 
Dann jetzt nochmal für Morphi:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
procedure TForm1.Button1Click(Sender: TObject);
Var
 A : String;
 B : Char;
 Index : Integer;
 Ende : Integer;
 laenge : Integer;
begin
 A := Edit2.Text;
 laenge := Length(A); // Damit Length nur einmal aufgerufen werden muss
 Ende := (laenge div 2)+1;
 For Index := laenge downto ende do begin
   B := A[laenge+1-Index];
   A[laenge+1-Index] := A[Index];
   A[Index] := B;
 end;
 Edit1.Text := A;
end;
Dominique
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 92



BeitragVerfasst: Fr 25.02.05 18:45 
Ist zwar nicht von mir, sondern von Ralph Friedmann (TeamB), aber bestimmt eine der schnellsten Methoden:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
function Reverse(S: string): string;
var
  I: integer;
  L: integer;
  P: PChar;
  Q: PChar;
begin
  L := Length(S);

  if L = 0 then
    Exit;

  SetLength(Result, L);

  P := PChar(Result);
  Q := PChar(S) + (L - 1);

  repeat
    P^ := Q^;
    Inc(P);
    Dec(Q);
  until Q = PChar(Pointer(S));
end;


und du nutzt es dann so:

ausblenden Delphi-Quelltext
1:
Edit1.Text := Reverse(Edit2.Text);					
Dominique
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 92



BeitragVerfasst: Fr 25.02.05 18:51 
Sprint hat folgendes geschrieben:

Unter Delphi 7 gibt es die Funktion AnsiReverseString. Unter Delphi 5 nicht. Bei Delphi 6 weiß ich es nicht.


leider ist's mit stringfunktionen bis delphi 7 nicht so üppig.

ein tip an morphi: guck doch zwischendurch mal in die delphi hilfe :roll:
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Fr 25.02.05 18:53 
Ok, wir geben uns geschlagen :) Ist ca. 4 mal schneller als Jasocul's korrigierte Methode von uall ;)
// Edit: und ca. 20% schneller als meine, wenn man dort noch die Länge zuvor in einer Variablen speichert ;(