in reply to slow startup for some common modules? (autodie, Pod::Usage, Getopt::Long))

G'day almr,

I made verbatim copies of your four scripts and ran them using time (I don't have hyperfine). I performed multiple runs on each; I got results that were of the same order of magnitude as yours. So, there's nothing problematic with your system in this regard.

I see some workarounds have been suggested. I use the three modules you tested in most of my $work scripts; I've never had any comments about slowness. I don't imagine any of your users will complain that a script took 100ms to start running. :-)

You'll no doubt be loading additional modules which will increase the startup time. Keep an eye on what you're importing.

# Import nothing -- fastest but requires additional coding use Some::Module (); ... Some::Module::some_function(); Some::Module::other_function(); # Import just the parts you intend to use -- still fast; less coding use Some::Module qw{some_function other_function}; ... some_function(); other_function(); # Import everything -- could be very slow use Some::Module; ... any_function();

If you're really concerned about startup speeds, put something like this at the start of your script:

BEGIN { print "Loading app (could take a second or two) ...\n"; }

Then, when it only takes 100ms or so, your users should be very pleased.

— Ken

Replies are listed 'Best First'.
Re^2: slow startup for some common modules? (autodie, Pod::Usage, Getopt::Long))
by almr (Beadle) on Jan 06, 2023 at 22:54 UTC
    Thanks for taking a look. For a standalone "application", 100msec for sure doesn't matter. However, if (1) I'm calling several perl scripts inside bashrc, or (2) a script is invoked after every command (PROMPT_COMMAND in bash) or (3) a script runs in an editor on every keystroke (see the vim-like editor Kakoune), those 100msec add up. 10 scripts taking 100msec each result in a 1sec delay before being able to type anything, which is of course unacceptable.

      I really wonder why such a program would need the full option processing powers of Getopt::Long, if at all.

      Options are most useful for programs that are used for several related but different actions. In particular cases 2 and 3 above I would stick to a dedicated program.

      Also, perl -s is even faster than Getopt::Std.