I'm developing some CGI scripts and I'm trying to find a solution to decrease the "starting time" produced when I import a lot of modules with "use".
- Make sure this is actually your bottleneck
- Only import modules you need
- Make sure you aren't having to search a zillion places to find them
- Consider having processes with costly startup run hot (e.g. as a daemon) and use lighter weight CGI scripts to start them
- Look into various accelerators rather than doing full CGI from a shell (depends on what you're using to serve the pages).
Consider using CGI::Fast in order to start one perl process to handle multiple requests. It took me very little effort to change some of my big CGI scripts over to CGI::Fast. Unlike mod_perl, it's very easy to run CGI::Fast on hosting sites because you can restart your scripts without restarting Apache (at least that's what my hoster told me when I asked for mod_perl).
You could always try the less pragma:
use less 'starttime';
Of course, that is system dependent*
. Your best bet is to use mod_perl or one of the FastCGI modules: CGI::Fast, FCGI, etc.
If it needs to be fast from the commandline you may want to move to a client/server architecture (which is all FastCGI is).
*
warning no systems currently implement a starttime option for the less pragma.
The solutions are nice, but the scripts I'm working runs both in console and CGI mode checking if some typical HTTP environment variables are present.
In "console mode" they dump the data "normally", and in "html mode" they make some realtime replacements and send other HTTP headers to the client.
I'd like to improve the startup time in both cases.. more tips? :)
thx!
Well, others already have suggested that CGI might be your problem here, so I'll consider that you can't remove CGI from the picture.
You might want to consider this old article. Apparently one source of slow startup time is a huge @INC so consolidating everything in a short PERL5LIB seem to help tremendously (that seems to be a fair assumption, but I never tried it).
Alternatively (or additionally), if you don't mind paying some price at run time you can use Class::Autouse
Enjoy!
Try using SpeedyCGI or Persistent Perl.
Both implement roughly the same idea: Instead of the Perl interpreter, they a wrapper that parses the program and keep it in memory, thus saving the time required for initializing the interpreter and parsing on every run.
This should work fine with the dual-environment setup you described which would/might not be the case when using CGI::Fast or mod_perl.
EDIT If this helps, fine. If it doesn't, you'll have to measure where your script spends its run-time.