Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re^2: Too Many IDs

by The_Dj (Sexton)
on Jan 09, 2020 at 13:49 UTC ( #11111246=note: print w/replies, xml ) Need Help??

in reply to Re: Too Many IDs
in thread Too Many IDs

Hi, Ken.

Yeah, I should have mentioned that both sn and id are* unique keys.

And thanks for the benchmark pointers. I'll keep this in mind when I get to optimizing.

Sadly I tend to loose most of my runtime to DB calls and some 3rd party .exe's, but I will give map a close scrutiny.

* Except one isn't really, but is treated as such for other reasons. My job can be 'fun'

Replies are listed 'Best First'.
Re^3: Too Many IDs
by kcott (Archbishop) on Jan 10, 2020 at 01:58 UTC

    If parts of your application (DB, EXEs, etc.) are taking seconds or milliseconds to run, spending time optimising map to save a few micro- or nanoseconds is unlikely to be worth the effort.

    Another consideration is whether this is a short-lived application that's run multiple times or a long-lived application that's run with multiple iterations.

    There are some JIT (just-in-time) possibilities that may be worth consideration. If you end up with a lot of get_X_for_Y() subs, and some are only called infrequently, you can create mappings just when they're needed; something like this using state (which requires v5.10):

    { my %dat_by_id = ...; sub get_X_for_Y { my ($Y) = @_; state $map_Y_to_id = { map +($_->{Y} => $_->{id}), values %dat_by_id }; return $dat_by_id{$map_Y_to_id->{$Y}}{X}; } sub get_X_for_Z { ... } sub get_V_for_W { ... } ... }

    Note that the anonymous block gives %dat_by_id lexical scope such that it is only visible to the subs; while the subs themselves are visible to, and accessible from, the entire script. This prevents inadvertent changes to %dat_by_id which could introduce bugs which are hard to track down.

    Also note that the placement of the above code within your script would be important. The assignment to %dat_by_id should occur before any calls to get_?_for_?() are made. An alternative to this would be to use some combination of BEGIN, INIT, etc. blocks; I don't know enough about your code to comment further on this; see perlmod: BEGIN, UNITCHECK, CHECK, INIT and END for more about these. There would, no doubt, be other ways to handle this but, as previously stated, I don't know enough about your code to offer further advice.

    — Ken

      Thanks for the reply

      Yes I know that shaving a few milliseconds of map won't help when I loose 4 seconds per external .exe,
      When my code is 'complete', I'll be letting NYTProf do its' thing and then fix the red bits first ;-)

      And thanks for the code samples; Must admit it's nicer than using a global and then //=

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2023-05-30 20:33 GMT
Find Nodes?
    Voting Booth?

    No recent polls found