Entwickler-Ecke
Delphi Language (Object-Pascal) / CLX - Wie Code vereinfachen?
white-desert - Mi 29.08.07 07:16
Titel: Wie Code vereinfachen?
Hallo!
Wie kann ich den folgenden Code vereinfachen:
Delphi-Quelltext
1: 2: 3:
| ByteEmpty := 4 - (ByteWidth mod 4); if ByteEmpty = 4 then ByteEmpty := 0; |
Danke im Voraus! :-)
alzaimar - Mi 29.08.07 07:36
Delphi-Quelltext
1:
| ByteEmpty := (4 - ByteWidth mod 4) mod 4; |
Oder
Delphi-Quelltext
1: 2: 3: 4: 5: 6:
| Const cLookup : Array [0..3] Of Integer = (0,3,2,1);
Begin ByteEmpty := cLookup[ByteWidth mod 4]; ... |
K.A., was schneller ist.
Horst_H - Mi 29.08.07 10:56
Hallo,
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 ByteEmpty := 4 - (ByteWidth mod 4); If ByteEmpty = 4 then ByteEmpty := 0; write(ByteWidth:5,ByteEmpty:4);
ByteEmpty :=(ByteWidth-1) AND $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 - Mi 29.08.07 12:02
Dann müßte ein generelles
Delphi-Quelltext
1:
| ByteEmpty := ByteWidth and $03; |
am schnellsten sein.
alzaimar - Mi 29.08.07 12:08
OlafSt hat folgendes geschrieben: |
Dann müßte ein generelles
Delphi-Quelltext 1:
| ByteEmpty := ByteWidth and $03; |
am schnellsten sein. |
Dann stimmt das Ergebnis aber nicht.
white-desert - Mi 29.08.07 12:17
Danke für eure schnellen Antworten!
Horst_H - 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;
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
Entwickler-Ecke.de based on phpBB
Copyright 2002 - 2011 by Tino Teuber, Copyright 2011 - 2026 by Christian Stelzmann Alle Rechte vorbehalten.
Alle Beiträge stammen von dritten Personen und dürfen geltendes Recht nicht verletzen.
Entwickler-Ecke und die zugehörigen Webseiten distanzieren sich ausdrücklich von Fremdinhalten jeglicher Art!