Compare strings A and B as numbers without explicitly converting them to machine numbers.
-
Ich schaue mir gerade den Sourcecode von sort an (coreutils-8.21).
Dabei verstehe ich folgendes Kommentar nicht, kann mir das jemand erklären?
// sort.c // dasda /* Compare strings A and B as numbers without explicitly converting them to machine numbers. Comparatively slow for short strings, but asymptotically hideously fast. */ // bisda static int numcompare (char const *a, char const *b) { while (blanks[to_uchar (*a)]) a++; while (blanks[to_uchar (*b)]) b++; return strnumcmp (a, b, decimal_point, thousands_sep); } // strnumcmp.c /* Externally-visible name for numcompare. */ int _GL_ATTRIBUTE_PURE strnumcmp (char const *a, char const *b, int decimal_point, int thousands_sep) { return numcompare (a, b, decimal_point, thousands_sep); } // und: /* Compare strings A and B as numbers without explicitly converting them to machine numbers, to avoid overflow problems and perhaps improve performance. DECIMAL_POINT is the decimal point and THOUSANDS_SEP the thousands separator. A DECIMAL_POINT of -1 causes comparisons to act as if there is no decimal point character, and likewise for THOUSANDS_SEP. */ static inline int _GL_ATTRIBUTE_PURE numcompare (char const *a, char const *b, int decimal_point, int thousands_sep) { ...
(C++-Style-Kommentare von mir)
-
Du hast Zahlen als Strings gegeben und vergleichst sie ohne sie zu konvertieren.
Btw. Stelle bitte eine konkrete Frage! Was vestehst du nicht?
-
knivil schrieb:
Du hast Zahlen als Strings gegeben und vergleichst sie ohne sie zu konvertieren.
Soweit ist alles klar.
Btw. Stelle bitte eine konkrete Frage! Was vestehst du nicht?
Den Nebensatz:
Comparatively slow for short strings, but asymptotically hideously fast
Aus meiner Sicht wäre es besser, die Zeilen vorzubereiten. Also trimmen, in (Pseudo-)Binärdarstellung bringen (also 9 chars in 32 Bits zusammenfassen, Exponent nach vorne bringen, etc.) und dann das zu vergleichen.
Ich sehe nicht, wie das asymptotisch langsamer ist. Und "hideously fast" ist ja ganz extrem. Ich muss irgendwas übersehen
-
Also trimmen
Wird gemacht, da blanks uebersprungen werden.
Exponent nach vorne bringen, etc
Ja, wie wenn du nicht weisst wo das ende ist?
Ich muss irgendwas übersehen
Du hast den interessanten Teil durch ... ersetzt, naemlich wie letztendlich verglichen wird.
Der eigentliche Knackpunkt ist wohl eher, dass keine Konvertierung der Zahl vorgenommen wird, anhand der Trennzeichen zeitig eine Entscheidung getroffen werden kann und beliebig grosse/kleine Zahlen verarbeitet werden koennen.