Autor Beitrag
white-desert
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 16



BeitragVerfasst: Mi 29.08.07 07:16 
Hallo!

Wie kann ich den folgenden Code vereinfachen:
ausblenden Delphi-Quelltext
1:
2:
3:
  ByteEmpty := 4 - (ByteWidth mod 4);
  if ByteEmpty = 4 then
    ByteEmpty := 0;


Danke im Voraus! :-)
alzaimar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2889
Erhaltene Danke: 13

W2000, XP
D6E, BDS2006A, DevExpress
BeitragVerfasst: Mi 29.08.07 07:36 
ausblenden Delphi-Quelltext
1:
ByteEmpty := (4 - ByteWidth mod 4mod 4;					

Oder
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
Const
  cLookup : Array [0..3Of Integer = (0,3,2,1);

Begin
  ByteEmpty := cLookup[ByteWidth mod 4];
...

K.A., was schneller ist.

_________________
Na denn, dann. Bis dann, denn.


Zuletzt bearbeitet von alzaimar am Mi 29.08.07 11:28, insgesamt 1-mal bearbeitet
Horst_H
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1654
Erhaltene Danke: 244

WIN10,PuppyLinux
FreePascal,Lazarus
BeitragVerfasst: Mi 29.08.07 10:56 
Hallo,

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
var
  ByteEmpty,
  ByteWidth : integer;
begin
for ByteWidth := 0 to 16 do
  begin
  //Original
  ByteEmpty := 4 - (ByteWidth mod 4);
  If ByteEmpty = 4 then
   ByteEmpty := 0;
  write(ByteWidth:5,ByteEmpty:4);

  //Oder mal so.
  ByteEmpty :=(ByteWidth-1AND $3 XOR $3;
  writeln(ByteEmpty:6);
  end;
end.


Auch keine Ahnung was schneller ist. mod 4 wird der Compiler durch AND $3 ersetzen und ist damit ohnehin sehr schnell.
Hauptsache das IF ist weg.

Gruß Horst.
OlafSt
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 486
Erhaltene Danke: 99

Win7, Win81, Win10
Tokyo, VS2017
BeitragVerfasst: Mi 29.08.07 12:02 
Dann müßte ein generelles

ausblenden Delphi-Quelltext
1:
ByteEmpty := ByteWidth and $03;					


am schnellsten sein.

_________________
Lies, was da steht. Denk dann drüber nach. Dann erst fragen.
alzaimar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2889
Erhaltene Danke: 13

W2000, XP
D6E, BDS2006A, DevExpress
BeitragVerfasst: Mi 29.08.07 12:08 
user profile iconOlafSt hat folgendes geschrieben:
Dann müßte ein generelles

ausblenden Delphi-Quelltext
1:
ByteEmpty := ByteWidth and $03;					


am schnellsten sein.

Dann stimmt das Ergebnis aber nicht.

_________________
Na denn, dann. Bis dann, denn.
white-desert Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 16



BeitragVerfasst: Mi 29.08.07 12:17 
Danke für eure schnellen Antworten!
Horst_H
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1654
Erhaltene Danke: 244

WIN10,PuppyLinux
FreePascal,Lazarus
BeitragVerfasst: Mi 29.08.07 15:46 
Hallo,

nur mal so am Rande mit Freepascal getestet.
Erste Version mit mod 4 gerechnet alle anderen Versionen mit AND $3.
CPU Takte beim Pentium M 1.7 Ghz, Schleifen durchläufe: cLoop = 100.000.000;
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
  Ausgangs version     
58.735 Takte mod,div  ist nun einmal lahm

  ByteEmpty :=(ByteWidth-1) AND $3 XOR 3 ; 
4.250 Takte 

  ByteEmpty := (4 - (ByteWidth AND  3))and 3;
4.947 Takte

  ByteEmpty := cLookup[ByteWidth AND 3];
2.890 Takte


Lookup in dieser Minitabelle war mit Abstand das schnellste

Gruß Horst