http://www.perlmonks.org?node_id=145527


in reply to Re: RFC CGI.pm refactoring
in thread RFC CGI.pm refactoring

I use CGI.pm all the time, but I only use a very minor portion of the module (...) So why should I pull in the whole of CGI.pm, especially when web programming should be as fast as possible?

I use Perl all the time but I only use a very minor portion of the language... you get the idea ;--)

Basically the extra functionality comes for free. Have you looked at the code of CGI.pm? Unless you use a method it is never loaded. It uses tons of clever tricks, its own version of Exporter (saving you 300 lines), and under 700 lines of "real" code, the rest being loaded on demand.

Are there any benchmarks of CGI::Simple against CGI?

Replies are listed 'Best First'.
Re: Re: Re: RFC CGI.pm refactoring
by tachyon (Chancellor) on Feb 15, 2002 at 11:53 UTC

    Yes there are. The test suite contains just under 1000 unit tests. This includes the entire CGI.pm CGI related test suite plus just under 100 concordance tests that have the basic form:

    use CGI; use CGI::Simple; use Test; # make a CGI environment; $ENV{'BLAH'} = .... [snip] my $qc = new CGI; my $qs = new CGI::Simple; my @params = qw(blah blah); my $qc_scalar = $qc->some_method(@params); my $qs_scalar = $qs->some_method(@params);; my @qc_array = $qc->some_method(@params);; my @qs_array = $qs->some_method(@params);; ok( $qs_scalar, $qc_scalar ); ok( join'',@qs_array, join'',@qc_array ) # continues for all common methods

    Concordance with CGI.pm 2.78 is 100%. CGI::Simple loads under 300 lines by default and uses Selfloader. As it is inherently OO it does not use Exporter at all and is considerably faster than CGI.pm when called OO. The wrapper module that gives you a functional interface CGI::Standard uses a couple dozen lines of code to make the namspace exports either on demand (using an AUTOLOAD routine) or as single methods or method sets.

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Re: Re: RFC CGI.pm refactoring
by tachyon (Chancellor) on Feb 15, 2002 at 14:56 UTC

    As requested by /msg from mirod on Benchmarking CGI::Simple is just under 3 times faster performing the most common function - getting the value of a param. It is variably faster on other common operations. Load times are more difficult to evaluate accurately. I would be interested in what you find.

    use Benchmark; use CGI; use CGI::Simple; $ENV{'QUERY_STRING'} = 'foo=bar&baz=boo'; $q = new CGI; $s = new CGI::Simple; timethese(250000, {'CGI' =>'$q->param("baz")', 'Simple' => '$s->param( +"baz")'}); __DATA__ Benchmark: timing 250000 iterations of CGI, Simple... CGI: 27 wallclock secs (27.19 usr + 0.00 sys = 27.19 CPU) @ 91 +94.56/s (n=250000) Simple: 10 wallclock secs ( 9.99 usr + 0.00 sys = 9.99 CPU) @ 25 +025.03/s (n=250000)

    Update

    For a more in depth analysis see CGI::Simple vs CGI.pm - Is twice as fast good enough?

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      Would you please benchmark this again?
      use Benchmark; use CGI qw/:cgi /; use CGI::Simple; $ENV{'QUERY_STRING'} = 'foo=bar&baz=boo'; $q = new CGI; $s = new CGI::Simple; timethese(250000, {'CGI' =>'$q->param("baz")', 'Simple' => '$s->param( +"baz")'});
      Sorry, but comparing fully blown CGI while forcing it to load all is quitely unfair in this case while CGI.pm has an option to run in a different manner.

      Have a nice day
      All decision is left to your taste
        And even then, with this test, while it's fair in how fast it accesses, the real concern would be the speed in breaking down the query string. I propose a better test would be :
        use Benchmark; use CGI qw/:cgi /; use CGI::Simple; $ENV{'QUERY_STRING'} = 'foo=bar&baz=boo'; timethese(250000, {'CGI' => '$q = new CGI; $q->param("baz")', 'Simple' => '$s = new CGI::Simple; $s->param( +"baz")'});

        -----------------------------------------------------
        Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
        "I can see my house from here!"
        It's not what you know, but knowing how to find it if you don't know that's important

        Actually you completely fail to understand how CGI.pm works. When you use CGI; you import 0 methods. The methods are available via the OO interface and compiled on demand. When you use CGI qw/:cgi/; you import and compile the basic cgi methods (which you would otherwise not do with a straight use CGI;). As we are making OO calls this is a complete waste of time. Running your example you can see that the time remains essentially the same as expected (loading the extra methods once (with the use CGI qw/:cgi/;) adds about a second to the load time but this is a once off price and we are then looping.

        use Benchmark; use CGI qw/:cgi /; use CGI::Simple; $ENV{'QUERY_STRING'} = 'foo=bar&baz=boo'; $q = new CGI; $s = new CGI::Simple; timethese(250000, {'CGI' =>'$q->param("baz")', 'Simple' => '$s->param( +"baz")'}); __DATA__ Benchmark: timing 250000 iterations of CGI, Simple... CGI: 29 wallclock secs (27.53 usr + 0.00 sys = 27.53 CPU) @ 90 +81.00/s (n=250000) Simple: 10 wallclock secs ( 9.99 usr + 0.00 sys = 9.99 CPU) @ + 25025.03/s (n=250000)

        cheers

        tachyon

        s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print