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

Is language really irrelevant to CGI performance on a typical webserver? I was playing around with microbenchmarks with lighttpd on my localhost and got a huge difference between Perl and C++ CGI programs. But deployed on a remote Apache 1.3 system there was little difference. C++ fastcgi on my localhost was also a few times faster than Perl fastcgi. However, my web host doesn't support fastcgi so I couldn't extend the comparison.

I wrote a few little programs: 1) Hello World. 2) Print a list of 500 random numbers. 3) Read in and reverse a file. I made Perl CGI versions (CGI::Simple), C++ cgi vesions (cgicc), Perl FCGI.pm versions, and C++ fastcgi vesions.

I benchmarked with "http_load -parallel 1 -seconds 20". I also tried parallel=2. I ran both lighttpd and http_load on my localhost. The results, measured in fetches per second, were fairly consistent across each program and with different http_load options: C++ CGI is 13 times as fast as Perl CGI. C++ fastcgi is 4 times as fast as Perl FCGI. C++ fastcgi is 5 times as fast as C++ CGI. Perl FCGI is 15 times as fast as Perl CGI.

I then deployed the CGI programs to a remote Apache 1.3 server. This host does not support fastcgi, so testing those versions was not an option. Performance between the Perl and C++ CGI programs was close to identical. The C++ versions would be, like, 1.13 times as fast at the most.

Is there an explanation for the lack of performance difference on the remote host, but an extreme performance difference on my desktop? Do you think there would actually be a scalability payoff to using C/C++ for performance critical bits in a CGI environment?

This is really just idle musing. Alas, I do not actually have any scalability problems.

  • Comment on performance questions regarding cgi & fastcgi with Perl and C++

Replies are listed 'Best First'.
Re: performance questions regarding cgi & fastcgi with Perl and C++
by Joost (Canon) on Aug 22, 2007 at 15:21 UTC
    For those programs, the runtime is probably mostly determined by the IO speed (i.e. disk and network speed), since both perl and C++ should be able to do those tasks faster than the disk/network would be able to process it.

    A local network connection, though, may be fast enough so that real processing time makes a difference.*

    The big slowdown for perl CGI is the time it takes to load the interpreter and program code/modules, since CGI reloads everything for each request, but if your perl code is relatively small, and system load is low, disk caching etc may help bring down the load time if you run the same process repeatedly. (having a shared perllib instead of a static perl binary may help too, I guess).

    If your perl program (including the modules it uses) is relatively large, or you've got a lot of requests to handle, using a system (like fastcgi) where you have the process (or multiple processes) running in a loop, handling one request after another without reloading is usually a great benefit, easily capable of increasing the performance 10 to 100 times. Especially since continual processes can make caching of (intermediate) processing a lot easier and faster.

    * (update) AFAIK on linux at least, local (loopback) network connections can be shortcut - ignoring much of the network subsystem - that means they can be a lot faster than the maximum speed of the actual network system. In any case, I would guess that on many systems, a connection to localhost would not be dependent on the actual network hardware.

Re: performance questions regarding cgi & fastcgi with Perl and C++
by BrowserUk (Patriarch) on Aug 22, 2007 at 15:08 UTC
Re: performance questions regarding cgi & fastcgi with Perl and C++
by suaveant (Parson) on Aug 22, 2007 at 15:15 UTC
    You apparently don't have any free time problems, either ;)

    I would have to say that most often, "well written" compiled C++ is going to be faster than Perl... though things like mod_perl and fastcgi help level the playing field. What I would like to see is some solid benchmarking of the time to write a moderately complex cgi in C++ versus Perl. In my experience it will typically take at least three times as long to write the C++... at least. This is where the true savings come into play, since these days programmers (typically) cost more than hardware.

                    - Ant
                    - Some of my best work - (1 2 3)

Re: performance questions regarding cgi & fastcgi with Perl and C++
by jbert (Priest) on Aug 22, 2007 at 16:07 UTC
    Alas, I do not actually have any scalability problems.

    That's the rub.

    As I understand it, the received wisdom is that a constant performance factor doesn't really help you anyway when scaling (you're walking an exponential curve - a constant factor won't help that much). What helps you is having rapid development, so you can remove bottlenecks as you find them (and move onto the next bottleneck).

    Basically, what you're after is (horizontal) scalability, not performance. And you can achieve the former more rapidly in a dynamic language (perl, python, pick your poison) than a stricter language (C++, java, C#).

    Some notes from a talk by youtube on this: http://kylecordes.com/2007/07/12/youtube-scalability/. In particular: "YouTube is coded mostly in Python. Why? “Development speed critical”

    (Of course, there are limits. Having 30 servers instead of 300 is useful. But tweaking for performance (whether that be writing fast, less maintainable perl code or moving stuff to C/C++) generally makes your system less flexible, so is something you want to delay as long as possible).

Re: performance questions regarding cgi & fastcgi with Perl and C++
by perrin (Chancellor) on Aug 22, 2007 at 15:44 UTC

    The most likely reasons for the difference are that the network slowness of a remote test drowns out small speed differences (you said C++ was 4 times as fast, but I bet the actual time difference is quite small), or that your remote host has a faster version of perl than your local machine (or a slower C++ compiler).

    There is nearly always a performance advantage to writing things in C, but not a scalability one. Scalability has to do with being able to add more machines without changing your application.

Re: performance questions regarding cgi & fastcgi with Perl and C++
by TheDauthi (Sexton) on Aug 22, 2007 at 23:25 UTC
    For plain CGI, it does. Loading a full interpreter, spawning it, loading modules and so forth each time is going to be slower than kicking off a single process that spits out data. Basically, perl's setup time is significantly longer, it has to do all of the things the C++ one does and more, like scan @INC for modules.
    However, for an environment that already has a perl interpreter ready (mod_perl, fastcgi, or perlex), and especially one with pre-loaded modules, I'd be somewhat surprised to find a significant difference. Some difference, but not a large one (and I would -not- bet either way on a complex script).
Re: performance questions regarding cgi & fastcgi with Perl and C++
by girarde (Hermit) on Aug 22, 2007 at 17:27 UTC
    Well, we don't know a few important things, like local hardware versus remote hardware, and also, how "remote" is remote? If the pipe to the Apache 1.3 box is small enough, everything will seem to run at about the same speed.
Re: performance questions regarding cgi & fastcgi with Perl and C++
by andreas1234567 (Vicar) on Aug 24, 2007 at 12:14 UTC
    Is language really irrelevant to CGI performance on a typical webserver?
    1. Hello World.
    2. Print a list of 500 random numbers.
    3. Read in and reverse a file.
    Just a side-note: I would stop for a moment and think of how relevant those tasks (and hence benchmarks) are for the overall performance of a typical webserver. nferraz writes in The problem with premature optimization...:
    you could make it instantaneously-fast, or even remove it, and it wouldn't have a significant impact on the global performance at all.
    I'm not saying the choice of language is irrelevant to CGI performance. I'm just saying that unless CGI performance is a bottleneck on a webserver, there are few (if any) reasons to optimize it. It all comes down to profiling where your application spends most of its time, and work on those specific areas.
    --
    Andreas
      Hi, probably my comment comes a bit late. I consider the study should drop completely Apache server which is less performant as lightppd and you should focus on comparing remote application running on lightppd. It is obvious an interpreted language as Python will never do as well as C++ did, does and will do forewer... ;-)