in reply to Re^2: Sort the file names a list
in thread Sort the file names a list

ikegami, I know you already know it, but reading your post somebody could thing that using the your GRT code is a good idea because it is the faster solution when actually it is not!

It is not good because using substr() to extract the sorting keys is very weak. If for instance, the file names extensions are changed, it will extract incorrect keys and, what is worst, without reporting any warning or error to the user!

Even if slower, using a regular expression to extract the keys is probably the best solution.

In any case, there is a faster (not recommendable either) method...

... use Sort::Key qw(keysort); ... cmpthese(-3, { ... sk => ' use strict; use warnings; my @sorted = keysort { substr($_, -(19+20), 19) } @::LogFileLi +st; ', });
that on my computer runs as...
Rate memoized st grt naive sk memoized 8784/s -- -26% -51% -54% -58% st 11894/s 35% -- -33% -38% -43% grt 17794/s 103% 50% -- -7% -14% naive 19207/s 119% 61% 8% -- -7% sk 20714/s 136% 74% 16% 8% --
Note also that on my hardware, naive is actually faster than grt.

Replies are listed 'Best First'.
Re^4: Sort the file names a list
by ikegami (Patriarch) on Sep 01, 2009 at 13:58 UTC

    What?

    First of all, do you have a problem with GRT or substr? You said GRT, but you talk of the substr I used for key extraction in all methods.

    Secondly, if the spec changes, of course the code will need to be changed. I don't see that as a problem. Switching to using a regex match is not going to help this.

    So, no, I don't "already know it".