FAQ 3.19 How can I make my CGI script more efficient?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

This is an excerpt from the latest version perlfaq3.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .


3.19: How can I make my CGI script more efficient?

    Beyond the normal measures described to make general Perl programs
    faster or smaller, a CGI program has additional issues. It may be run
    several times per second. Given that each time it runs it will need to
    be re-compiled and will often allocate a megabyte or more of system
    memory, this can be a killer. Compiling into C isn't going to help you
    because the process start-up overhead is where the bottleneck is.

    There are two popular ways to avoid this overhead. One solution involves
    running the Apache HTTP server (available from http://www.apache.org/ )
    with either of the mod_perl or mod_fastcgi plugin modules.

    With mod_perl and the Apache::Registry module (distributed with
    mod_perl), httpd will run with an embedded Perl interpreter which
    pre-compiles your script and then executes it within the same address
    space without forking. The Apache extension also gives Perl access to
    the internal server API, so modules written in Perl can do just about
    anything a module written in C can. For more on mod_perl, see
    http://perl.apache.org /

    With the FCGI module (from CPAN) and the mod_fastcgi module (available
    from http://www.fastcgi.com/ ) each of your Perl programs becomes a
    permanent CGI daemon process.

    Both of these solutions can have far-reaching effects on your system and
    on the way you write your CGI programs, so investigate them with care.



The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in

Site Timeline