Autor Beitrag
ene
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 779
Erhaltene Danke: 1

Vista, XP, W2K
Delphi, .Net, Deutsch und Englisch
BeitragVerfasst: Mi 04.04.07 14:42 
Tach,

ich überlege mir gerade einen vernünftigen logischen Ablauf für folgendes Vorhaben:

Ich habe einen Wert L, diesen halbiere ich und möchte jetzt einmal von L[0]-L[Halbe] und einmal von L[Max]-L[Halbe] laufen. Sicher kann ich das in 2 Schleifen lösen, aber ich denke mir das geht doch einfacher. Anregungen deutlich erwünscht ;)
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mi 04.04.07 14:48 
Moin!

Was soll denn bei zwei Schleifen mit unterschiedlichen Grenzen optimierbar sein... :gruebel: :nixweiss:

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
ene Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 779
Erhaltene Danke: 1

Vista, XP, W2K
Delphi, .Net, Deutsch und Englisch
BeitragVerfasst: Mi 04.04.07 14:53 
Hm, missverständlich ausgedrückt. Ok, ich möchte nicht so etwas schreiben:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
i:= 0;
Repeat
  //machwas
  Inc(i);
Until i >= L/2;

i:= L;
Repeat
  //machwas
  Dec(i);
Until i <= L/2;


Sondern bin am Überlegen, wie ich die 2 Schleifen unter einen Hut bekomme.
freak4fun
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 604
Erhaltene Danke: 4

Win 7 Pro
VS 2013 Express, Delphi, C#, PHP, Java
BeitragVerfasst: Mi 04.04.07 14:55 
Du kannst die Prüfung von 0 bis L laufen lassen und in der Schleife prüfen wie groß der wert ist. Zum Beispiel mit:
ausblenden Delphi-Quelltext
1:
2:
if i >= L div 2 then
  ...

Allerdings lohnt das nicht für kleine Sachen. Aber es wäre eine Möglichkeit. :gruebel:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
i:= 0; 
half := L / 2;
Repeat 
  if i < half then
    begin
      //machwas 
    end
  else
    begin
      //mach was anderes
    end;
  Inc(i); 
Until i >= L;


MfG
freak

_________________
"Ich werde auf GAR KEINEN Fall…!" - "Keks?" - "Okay, ich tu's."
i++; // zaehler i um 1 erhoehen


Zuletzt bearbeitet von freak4fun am Mi 04.04.07 14:59, insgesamt 1-mal bearbeitet
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mi 04.04.07 14:56 
Moin!

Was ist das Ziel: Performance, Eleganz, ... ? :nixweiss: ;)

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
ene Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 779
Erhaltene Danke: 1

Vista, XP, W2K
Delphi, .Net, Deutsch und Englisch
BeitragVerfasst: Mi 04.04.07 14:58 
Ähh, was? Ich muss i ja vorher belegen und damit weiß ich doch auch womit ich starte oder meinst du was anderes?

@Narses Eleganz. Und die kann bei einem Vielzeiler schon ausreichen, damit man den Code leichter versteht ;)
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mi 04.04.07 15:00 
Moin!

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
g := L div 2;
for i := 0 to L do
  if (i <= g) then

  else

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
ene Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 779
Erhaltene Danke: 1

Vista, XP, W2K
Delphi, .Net, Deutsch und Englisch
BeitragVerfasst: Mi 04.04.07 15:03 
Ah, geht leider nicht. Ich muss wirklich von Max DownTo Halbe laufen und nicht von Halbe To Max. :(
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mi 04.04.07 15:06 
Moin!

Ehrlich gesagt finde ich die beiden repeat-Schleifen absolut elegant... ;)

Was wird´s, QuickSort für die Schule? :P

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
freak4fun
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 604
Erhaltene Danke: 4

Win 7 Pro
VS 2013 Express, Delphi, C#, PHP, Java
BeitragVerfasst: Mi 04.04.07 15:08 
user profile iconNarses hat folgendes geschrieben:
Ehrlich gesagt finde ich die beiden repeat-Schleifen absolut elegant... ;)


Da stimme ich Narses vollkommen zu! :zustimm:

_________________
"Ich werde auf GAR KEINEN Fall…!" - "Keks?" - "Okay, ich tu's."
i++; // zaehler i um 1 erhoehen
ene Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 779
Erhaltene Danke: 1

Vista, XP, W2K
Delphi, .Net, Deutsch und Englisch
BeitragVerfasst: Mi 04.04.07 15:09 
Ein Algorithmus der 3D Daten entsprechend seines Bauprozesses verformt :P Dann lager ich die beiden Schleifen lieber in eine Procedure aus und übergebe die Werte.
noidic
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 851

Win 2000 Win XP Vista
D7 Ent, SharpDevelop 2.2
BeitragVerfasst: Mi 04.04.07 15:12 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
var 
  i,g : integer;
begin
  for i := 0 to L do
  begin
    if I < (L div 2) then
      g := i
    else
      g := L + ((L div 2 + 1) - i);

    (Machwas mit g)

  end;
end;


Müsste so gehen denke ich.

_________________
Bravery calls my name in the sound of the wind in the night...
ene Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 779
Erhaltene Danke: 1

Vista, XP, W2K
Delphi, .Net, Deutsch und Englisch
BeitragVerfasst: Mi 04.04.07 15:19 
Könnte klappen, ich geb morgen Feedback.
ene Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 779
Erhaltene Danke: 1

Vista, XP, W2K
Delphi, .Net, Deutsch und Englisch
BeitragVerfasst: Do 05.04.07 06:27 
Klappt, vielen Dank!
freak4fun
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 604
Erhaltene Danke: 4

Win 7 Pro
VS 2013 Express, Delphi, C#, PHP, Java
BeitragVerfasst: Do 05.04.07 07:36 
user profile iconene hat folgendes geschrieben:
Klappt, vielen Dank!

Ist aber nicht so performant wie deine erste Lösung. :roll: Welchen Grund hat es denn, dass du nur eine Schleife willst?

_________________
"Ich werde auf GAR KEINEN Fall…!" - "Keks?" - "Okay, ich tu's."
i++; // zaehler i um 1 erhoehen
ene Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 779
Erhaltene Danke: 1

Vista, XP, W2K
Delphi, .Net, Deutsch und Englisch
BeitragVerfasst: Do 05.04.07 07:41 
Spart ca. 20 Zeilen Code und ich mag absolut keine redundanz. Zur Beruhigung habe ich keinen nennenswerten Performanceunterschied. Beide Versionen brauchen 2 Sekunden.
noidic
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 851

Win 2000 Win XP Vista
D7 Ent, SharpDevelop 2.2
BeitragVerfasst: Do 05.04.07 07:45 
Sollte durch den Optimierer auch eigentlich keinen Performanceunterschied geben. Auch wenn ich die Lösung mit 2 Schleifen auch schöner finden, da lesbarer.

_________________
Bravery calls my name in the sound of the wind in the night...
oldmax
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 380


D3 Prof, D4 Prof
BeitragVerfasst: Do 05.04.07 08:41 
Hi
Also, ich würds folgendermaßen lösen...
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
Procdure Beispiel;
Var  x  :  Integer;
     y  :  Integer;
     max:  Integer;
Begin
  Max:=99;
  x:=0;
  Repeat
    x:=x+1;
    if y-1>Max-x then y:=max-x;
    ..... // mach was mit x und y x steigt, y fällt
  Until x>=Trunc(max/2);
end;


geht natürlich auch mit jeder anderen Schleife.....
Gruß oldmax

_________________
Zier dich nich so, ich krieg dich schon....