There are lots of ways to get about a 50% speed increase over an ST. Creating all of those tiny anonymous arrays isn't a speedy task. It does lead to rather simple and non-error-prone code, however.
One alternative that is as general purpose is to build a separate array of keys to sort on and then sort a list of indices from that:
my @sorted= do {
my @sorton= map { get_date($_) } @data;
my @indices= sort { $sorton[$a] cmp $sorton[$b] } 0..@data;
@data[@indices];
};
Another alternative that can be slightly faster but is a bit harder to customize is to combine the key to sort on with the original data into a single string in a reversible manner and use:
my @sorted= map { restore_orig_data($_) }
sort map { prepend_sort_key($_) } @data;
See also http://www.sysarch.com/perl/sort_paper.html or even http://www.perl.com/doc/FMTEYEWTK/sort.html.
-
tye
(but my friends call me "Tye")
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|