Re: sorting dates



JRS: In article <dccq76$22i$1@xxxxxxxxxxxxxxxxx>, dated Fri, 29 Jul
2005 03:43:07, seen in news:comp.lang.javascript, fox
<spamtrap@xxxxxxxxxxxxx> posted :
>
>
>comparing strings no doubt takes longer than comparing numbers.

Perhaps so. But the comparison operator should be implemented at the
ASM level or similar; so, for strings of the length in question, ISTM
reasonably likely that the overall time will be dominated by other parts
of the routine. If it matters, it should be tested.

With 8-character strings, using the quickest test loop I know, I find
the loop for comparison of equal 8-character Strings to be about 10%
slower than that for the corresponding Numbers, loop overhead being
about 20%. Other browsers are likely to differ.


>if all dates to be sorted are in YYYY-MM-DD format, and -- worst case,
>they are all in the first week of the same year and month, then the
>MINIMUM # of comparisons needed to be made to pass one as <> the other
>would be 10 (one per character as long as they are equal) - two week
>span, 9 -- etc... Unless, of course, JavaScript can compare "chunks" of
>strings in one "throw" (I used to do it in C -- up to 8 characters at a
>time -- typecast to longints -- really good way to speed up searches too)

IIRC, the PC CPU instruction set provides for efficient comparisons of
strings of 8-bit characters, and may do so, intentionally or otherwise,
foe 16-bit ones.


>oh... that little YYYYMMDD integer format -- it's called a "packed" date
>format (packing integers is a "time-honored programming tradition") --
>been around for considerably longer than JavaScript. Technically, the
>"digits" should be hexadecimal, but for sorting purposes, decimal digits
>work just as well with the extra added benefit of being easily readable
>in human terms.

It's not rightly called "packed". When discussing ISO 8601 formats, one
should use ISO 8601 terminology.

The string 'YYYYMMDD' is "Basic format".
The string 'YYYY-MM-DD' is "Extended format".

A Basic format string can (with unary +) be converted to a javascript
Number; but that is an IEEE Double, not an integer. Numbers in
javascript do not have decimal digits.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
.



Relevant Pages

  • Re: Some newbie questions - bare with me ;)
    ... This means that the symbol:abc will be ... This is unlike what happens with strings, ... * == this method is used for comparing tow objects basing on their contents. ... def y= value ...
    (comp.lang.ruby)
  • Re: Some optimization tale
    ... >> (and that the right algorithm makes indeed a difference). ... >Since Python does not have pointers, comparing characters within strings ... >requires pulling out the chars as separate strings. ... >truncate prefix to the length of item first. ...
    (comp.lang.python)
  • Re: Mergesort Vs Quicksort
    ... size of a record without comparing two records. ... A contiguous block of strings remains in the orig order. ... The comp function was called 1878269 times. ... recsize was called 3972636 times. ...
    (comp.programming)
  • Re: Some optimization tale
    ... A common surprise. ... Since Python does not have pointers, comparing characters within strings ... truncate prefix to the length of item first. ...
    (comp.lang.python)
  • Re: Summation, modulo and decimal
    ... if you know ahead of time how many decimal digits the given ... Multiplying by a suitable power of 10, then adding, and then ... is typically much more efficient than exponentiation, ... exponentiation, multiplies or divides, but would transform strings to ...
    (sci.math)