Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^3: Too Many IDs

by kcott (Archbishop)
on Jan 10, 2020 at 01:58 UTC ( [id://11111273]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^4: Too Many IDs
by Anonymous Monk on Jan 13, 2020 at 01:06 UTC

    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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-16 15:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found