![]() |
HexToInt optimieren
Hi,
Ich habe folgende 2 Funktionen:
Delphi-Quellcode:
Die 2. Version ist die schnellere, mir allerdings nicht schnell genug.
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; Hat jemand eine optimierte (assembler) Version von HexToInt oder eine Idee zur Optimierung? Danke! |
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; |
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