Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi HexToInt optimieren (https://www.delphipraxis.net/150707-hextoint-optimieren.html)

luke2 24. Apr 2010 16:03


HexToInt optimieren
 
Hi,

Ich habe folgende 2 Funktionen:
Delphi-Quellcode:
function HexToInt1(const S: string): Cardinal;
begin
  Result := StrToInt('$' + S);
end;

function HexToInt2(const S: string): Cardinal;
var
  I: Byte;
  C: Char;
begin
  Result := 0;
  for I := 1 to Length(S) do
  begin
    Result := Result * 16;
    C := S[I];
    case C of
      '0'..'9': Inc(Result, Ord(C) - Ord('0'));
      'a'..'f': Inc(Result, Ord(C) - Ord('a') + 10);
      'A'..'F': Inc(Result, Ord(C) - Ord('A') + 10);
      //else raise EConvertError.Create('No Hex-Number');
    end;
  end;
end;
Die 2. Version ist die schnellere, mir allerdings nicht schnell genug.
Hat jemand eine optimierte (assembler) Version von HexToInt oder eine Idee zur Optimierung?

Danke!

jfheins 24. Apr 2010 16:12

Re: HexToInt optimieren
 
Hmmm ... Ideen:
Du könntest das *16 durch shl 4 ersetzen - weis nicht ob das vll. automatisch passiert, aber naja.
Außerdem könntest du ein Array machen, in dem du direkt nachguckst. Lookup-Tabelle quasi.
Und statt inc() könntest du auch ein bitweises or nehmen.

Also sowas:
Delphi-Quellcode:
function HexToInt2(const S: string): Cardinal;
var
  I: Byte;
const
  Arr: Array[AnsiChar] = ....; // Vorher belegen mit den passenden Werten
begin
  Result := 0;
  for I := 1 to Length(S) do
    Result := (Result shl 4) or Arr[AnsiChar(S[I])];
end;

luke2 24. Apr 2010 16:19

Re: HexToInt optimieren
 
Danke, ist jetzt doppelt so schnell wie vorher! :)


Alle Zeitangaben in WEZ +1. Es ist jetzt 12:57 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz