![]() |
Practical mod_perl / HTML Book / | ![]() |
||
![]() |
||||
![]() |
![]() |
|||
![]() |
![]() |
|||
![]() |
||||
|
|
||||
![]() |
![]() |
|||
![]() |
13.6.1. The Overhead with Light Subroutines |
![]() |
||
![]() |
||||
![]() |
![]() |
![]() |
||
![]() |
||||
Let's do some benchmarking. We will start by using empty methods, which will allow us to measure the real difference in the overhead each kind of call introduces. We will use the code in Example 13-15.
package Book::LightSub;
use strict;
use Benchmark;
sub bar { };
timethese(1_000_000, {
method => sub { Book::LightSub->bar( ) },
function => sub { Book::LightSub::bar('Book::LightSub');},
});
The two calls are equivalent, since both pass the class name as their first parameter; function does this explicitly, while method does this transparently.
Here's the benchmarking result:
Benchmark: timing 1000000 iterations of function, method... function: 2 wallclock secs ( 1.36 usr + 0.05 sys = 1.41 CPU) method: 3 wallclock secs ( 2.57 usr + -0.03 sys = 2.54 CPU)
We see that the function call is almost twice as fast as the method call: 1.41 CPU clocks compared to 2.54. Why is this? With a function call we give Perl the fully qualified function name and set up its call stack ourselves by passing in the package (class) name. With a method call Perl must work out the package (class) name for itself, then search the inheritance tree to find the required method, then set up the call stack. So in the case of a method call Perl must do a lot more work and is therefore slower.
Perl 5.6.0 and higher do better method caching than older Perl versions. Book::LightSub->method( ) is a little bit faster (as it does better constant-folding magic), but not Book::LightSub->$method( ). The improvement does not address the @ISA lookup that still happens in either case.
 
Continue to:
mod_perl, modperl, Apache, perl, cgi, html, mod_perl, e-commerce, scalability, free, open source, OSS, apache, squid, high availability, modperl, linux, unix, Web, www, mod_perl, webserver, admin, apache, book, webmaster, tools, modperl, guide, docs, documentation, help, mod_perl, perl, information, apache, script, errata, eric cholet, perl, apache, mod-perl, stas bekman, mod_perl, cool, perl, Apache, performance, speed, choice
![]() |
Other projects to check out: meta-religion.com is for those interested in Religious, Spiritual and Esoteric Phenomena. i-want-a-better.com is a community of people discussing what they would like to be improved in their lives and things they use and interact with. You may also want to find a healer in your area or read articles on variety of topics.