in reply to Re: Re: Speeding up sort routines
in thread Speeding up sort routines

In that case, changing the comparison operator has changed the way your sort works (effectively breaking it).

To demonstrate, try this code:

($a, $b) = ( '16:46:36', '16:00:00'); print 'cmp : ', $a cmp $b, "\n"; print '<=> : ', $a <=> $b, "\n";

This prints

cmp : 1 <=> : 0

Which shows that Perl thinks that the two strings are the same when using <=>. When doing a string comparison, Perl uses all of the characters in the string, but when doing a numeric comparison, it can only use the digits at the start of the string. It stops when it finds the first non-digit. Had you been using -w then you would have got a warning explaining what you were doing wrong.

Also, doing a string comparison on dates in the format "Jun 28, 2001" is a bad idea as it will do an alphabetical sort - not in date order.

I'd recommend doing a first pass on the data and converting it to YYYYMMDDhhmm, which you can then do a numeric sort on.

--
<http://www.dave.org.uk>

Perl Training in the UK <http://www.iterative-software.com>

Replies are listed 'Best First'.
Re: Re: Re: Re: Speeding up sort routines
by Anonymous Monk on Jul 02, 2001 at 20:31 UTC
    I'm pointing this out even though you hinted at it. The reason the sort is so much faster with <=> is because perl thinks the two strings are equal and with all elements in a list being equal no swaps have to be made when sorting and with perl's sort algorithm less comparisons have to be made.