Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^6: How can I do a numeric sort on a substring? (context matters)

by swl (Parson)
on Jun 28, 2021 at 04:00 UTC ( [id://11134371]=note: print w/replies, xml ) Need Help??


in reply to Re^5: How can I do a numeric sort on a substring? (context matters)
in thread How can I do a numeric sort on a substring?

Out of curiosity I added some subs using Sort::Key. sort_key_natural is the natsort function from Sort::Key::Natural while sort_key_integer uses the ikeysort function from Sort::Key in tandem with substr.

The natsort approach is not particularly fast, but this is perhaps to be expected given it is a general purpose function (as are the unanchored regex approaches). I guess the integer key approach is faster as it takes advantage of direct string operations when building the keys, and then whatever optimisations Sort::Key uses internally.

I assume the differences in the order of the other approaches compared with Lanx's is due to the code being run on Strawberry perl 5.28. It would be interesting to know how the Sort::Key approaches go under a more recent Perl.

Edit: And now I look at the source code for Sort::Key::Natural, it is uses a regex approach to divide the string and pad out the numeric sections, so it is not surprising that it is slower than the other regex based approaches here. https://metacpan.org/dist/Sort-Key/source/lib/Sort/Key/Natural.pm#L34.

Replies are listed 'Best First'.
Re^7: How can I do a numeric sort on a substring? (context matters)
by salva (Canon) on Jun 28, 2021 at 09:15 UTC
    One of the reasons Sort::Key::Natural is relatively slow is because it tries to be correct!

    For instance, it can handle arbitrarily large numbers or Unicode.

      Agreed. A general solution that is correct for many cases is unlikely to be faster than a specialist solution that works for only one input configuration, such as is being tested here.

Re^7: How can I do a numeric sort on a substring? (context matters)
by kcott (Archbishop) on Jun 28, 2021 at 08:36 UTC

    G'day swl,

    See "Re^7: How can I do a numeric sort on a substring? [Benchmark: reworked and extended]". I've added sort_key_integer and sort_key_natural (made some guesses about the code) as well as a couple of additions of my own.

    "It would be interesting to know how the Sort::Key approaches go under a more recent Perl."

    I'm not seeing a huge difference between your output and my latest. SKn is at the slow end of the spectrum; SKi is by far the fastest (substantially faster on my system with Perl 5.34.0).

    — Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11134371]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-04-19 10:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found