AlfaProject has asked for the wisdom of the Perl Monks concerning the following question:

Hello What is run faster in cgi scripts bash commands or perl commands ? For example my $txt=`cat $filename`;print $txt or open FH,"<$filename";print while(<FH>);close FH; Another example is GET using LWP or `GET $html_page`; Thanks

Replies are listed 'Best First'.
Re: perl or bash in cgi scripts?
by marto (Cardinal) on Jan 06, 2011 at 11:44 UTC
      But benchmark the right thing.

      We are talking about a cgi-script where you can assume that it will serve clients that are on another machine (probably even on another network).

      Even finding out that a bash-version is twice as fast as a perl-version it means nothing when the network-overhead is a thousend times bigger.

      So if you want to benchmark, benchmark not the scripts but the impact it has on clients receiving responses from your script.

      Using Perl gives you easy access to query-parameters (CGI.pm), a debugger and endless ways to make your cgi-script talk to other systems (a database maybe?).

      If you are doing cgi use Perl.

      If cgi is too slow use mod_perl.

      Don't ever use a shell-script for that sort of stuff - unless maybe as an excercise in software archeology to see how previous generations used to do things.

        Perhaps I'm misunderstanding the question, but it seems to me that AlfaProject has a CGI script written in Perl and has specifically asked if using Perl to open a file (and use LWP) is 'faster' than using unix commands to achieve the same thing. From what I read from your reply you seem to think that they asked a more general question, sort of like the post title, 'which is faster bsah or perl in CGI scripts'.

        Benchmarking the script will determine which is 'faster', performance testing a application/interface is different from benchmarking, as you suggest.

        Obviously there are other things to take into account, some of which would require much more detailed information in order to advise properly.

Re: perl or bash in cgi scripts?
by cdarke (Prior) on Jan 06, 2011 at 13:22 UTC
    Maybe this is semantics, but last time I looked cat(1) was not a Bash built-in, it has nothing to do with Bash, it is a separate program.
    $ type cat cat is /bin/cat
    Recent versions of the korn shell do have a cat built-in. Perl anyway will not invoke a shell unless the command contains shell meta-characters. In your example $filename might contain meta-characters, we can't tell. You should take this into account when benchmarking, since if a shell is produced you will have two child processes instead of one.
Re: perl or bash in cgi scripts?
by locked_user sundialsvc4 (Abbot) on Jan 06, 2011 at 16:37 UTC

    Obviously, it is a trade-off.   When you incorporate shell-command calls directly into your Perl module, the sometimes-significant risk that you run is that (a) something might change in the underlying environment, and (b) that you might one day need to move the software to an environment where what you have done will no longer apply.

    I do not consider efficiency to be a valid consideration, but reliability, availability, and serviceability (what IBM likes to call “RAS”) most-definitely is.   So, tempting though it might be to “just go for Whatever Works,” sometimes that decision is very sorely and very soon regretted.

    Quite a few of the modules that are available in CPAN are, effectively, “operating-system abstraction layers.”   When installed on different operating systems, these modules actually install different code.   But they continue to work (insofar as possible or practical) “the same way.”   Such modules often turn out to be quite useful, but of course with varying degrees of success.

    For what it might be worth, I detest “shell scripting” and advocate against it wherever possible.   In my humble, the scripting capabilities of a shell are not designed to be “for serious programming,” and this is not the place where “serious programming” belongs.   I am, of course, perfectly aware that a one Mr. Korn and his followers would steadfastly disagree with me, and I respect that (and them).   I advocate that “you have a bountiful banquet of designed-to-be programming languages at your beck and call; so choose one and use it.”   (That is to say, “Choose Perl, o’course ...”)   ;-)

      Don't get me started (too late). The problem often is that shell programmers do not know enough about the shell they are using. cat(1) is an example, it is rairly required in shell scripts but often used, as is `ls` to get a list of files instead of using globbing. I could go on. ksh93 and Bash are very powerful these days but the features are hardly used.

      One difference with Perl is that users tend to make the effort to learn it (usually), but they don't bother learning their shell.

        Without cat how else would I be able to get this quadruple distilled list of files in the current working directory?

        [me@mybox~]$ for ff in $(echo * | cat); do > ls -ld $ff > done | awk '{print $NF}' RCS curl.tar index.html index.html.1 perl projects rsync_backup.sh sandbox ssh-to util
Re: perl or bash in cgi scripts?
by jffry (Hermit) on Jan 06, 2011 at 15:14 UTC

    Be sure to use mod_perl. Apparently there is also a mod_ bash, but it almost certainly does not have the same level of stability.

    As for benchmarking, using mod_log_config create a CustomLog with a LogFormat containing %D, which measures time to serve the request in microseconds.

      What about FastCGI and the corresponding module? Or is there overlap?

        I'm not ignoring this reply. I am merely ignorant of FastCGI, and can't speak on it. Hopefully, someone else can.