Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

$r-dir_config and speed

by jjhorner (Hermit)
on Jun 13, 2000 at 20:06 UTC ( [id://17904]=perlquestion: print w/replies, xml ) Need Help??

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

I'm updating my Apache module (available in code section) and I'm wondering if the $r->dir_config call checks the config file each time, or if it stores all of the key value pairs in a hash. Since one of my values (DefaultLimit) will be in the httpd.conf file, and one of them (TimeLimit) will be user supplied in an .htaccess file, should I just call them both (if they exist) and store in temp variables, or should I just call them using the $r->dir_config each time they are needed?

I know this is a mod_perl question and not specifically a perl question, but I still think it is worth looking into.

For instance:

if ($r->dir_config('TimeLimit')) { if ($r->dir_config('TimeLimit') < $r->dir_config('Defa +ultLimit')) { $time_to_die = $r->dir_config('TimeLimit'); } else { $time_to_die = $r->dir_config('DefaultLimit'); } } else { $time_to_die = $r->dir_config('DefaultLimit'); }

Or

my ($default, $time_to_die, $limit); $default = $r->dir_config('DefaultLimit'); $limit = $r->dir_config('TimeLimit') if ($r->dir_config('TimeLimi +t')); ($limit < $default) ? $time_to_die = $limit : $time_to_die = $def +ault;

Any ideas?

Replies are listed 'Best First'.
RE: $r-dir_config and speed
by jjhorner (Hermit) on Jun 13, 2000 at 21:18 UTC

    Never one to wait for an answer, I kept digging and finally decided to pull out the trusty Benchmark module:

    #!/usr/bin/perl -w use strict; use Benchmark; my %hash = ( DefaultLimit => 900, TimeLimit => 900, MODE => 'Off' ); sub by_hash { my $time_to_die; if ($hash{'TimeLimit'}) { if ($hash{'TimeLimit'} < $hash{'DefaultLimit'}) { $time_to_die = $hash{'TimeLimit'}; } else { $time_to_die = $hash{'DefaultLimit'}; } } else { $time_to_die = $hash{'DefaultLimit'}; } } sub by_var { my ($default, $time_to_die, $limit); $default = $hash{'DefaultLimit'}; $limit = $hash{'TimeLimit'} if ($hash{'TimeLimit'}); ($limit && ($limit < $default)) ? $time_to_die = $limit : $tim +e_to_die = $default; } timethese (100000, { hash => 'by_hash', var => 'by_var'});

    Yeilds this:

    [13:20:12 jhorner@gateway lib]$ ./test1.pl Benchmark: timing 100000 iterations of hash, var... hash: 5 wallclock secs ( 4.57 usr + 0.00 sys = 4.57 CPU) @ 21 +881.84/s (n=100000) var: 4 wallclock secs ( 3.74 usr + 0.00 sys = 3.74 CPU) @ 26 +737.97/s (n=100000) [13:20:24 jhorner@gateway lib]$

    This leads me to believe that I should use temp variables to store this data. Anyone have any comments, or suggestions?

    Has anyone else reached any other results?

    Thanks,

    J. J. Horner
    Linux, Perl, Apache, Stronghold, Unix
    jhorner@knoxlug.org http://www.knoxlug.org/
    
Re: $r-dir_config and speed
by plaid (Chaplain) on Jun 14, 2000 at 23:21 UTC
    First, a minor stylistic issue; the line
    ($limit < $default) ? $time_to_die = $limit : $time_to_die = $default;
    Is more often written
    $time_to_die = ($limit < $default) ? $limit : $default;
    But back to the matter at hand. In general, any value that has to be looked up in a hash or through a function is going to be slower than just grabbing a scalar. A quick benchmark I wrote:
    package Apache::Test; use strict; use Apache::Constants qw(:common); use Benchmark; use vars qw($r); # Not a good idea in general, but I couldn't # figure out how to pass parameters in a # benchmark. Works fine for benchmarking purposes. sub handler { $r = shift; $r->send_http_header; $r->print("by_dirconfig:",timestr(timeit(250000,\&by_dirconfig +)),"\n"); $r->print("by_var:", timestr(timeit(250000, \&by_var)), "\n"); return OK; } sub by_dirconfig { my $time_to_die; if ($r->dir_config('TimeLimit')) { if ($r->dir_config('TimeLimit') < $r->dir_config('Defa +ultLimit')) { $time_to_die = $r->dir_config('TimeLimit'); } else { $time_to_die = $r->dir_config('DefaultLimit'); } } else { $time_to_die = $r->dir_config('DefaultLimit'); } } sub by_var { my ($default, $time_to_die, $limit); $default = $r->dir_config('DefaultLimit'); $limit = $r->dir_config('TimeLimit') if ($r->dir_config('TimeL +imit')); $time_to_die = ($limit < $default) ? $limit : $default; } 1;
    Which leaves in my browser window:

    by_dirconfig:14 wallclock secs (15.28 usr + 0.03 sys = 15.31 CPU)
    by_var:11 wallclock secs (12.07 usr + 0.02 sys = 12.09 CPU)

    Conclusions are about the same as yours, just thought I'd add my results.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://17904]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (1)
As of 2024-04-18 23:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found