Re: [Templates] Performance on my new site is a drag

[prev] [thread] [next] [Date index for 2004/10/25]

From: merlyn (Randal L. Schwartz)
Subject: Re: [Templates] Performance on my new site is a drag
Date: 22:32 on 25 Oct 2004
>>>>> "Gavin" == Gavin Estey <gavin@xxxx.xxx> writes:

Gavin> package TimingContext;
Gavin> use base qw( Template::Context );
Gavin> use Time::HiRes qw( gettimeofday tv_interval );
Gavin> foreach my $sub (qw( process include )) {
Gavin>     *$sub = sub {
Gavin>         my $self     = shift;
Gavin>         my $template = ref $_[0] ? $_[0]->name : $_[0];
Gavin>         my $start    = [gettimeofday];
Gavin>         my $data     = eval "\$self->SUPER::$sub(\@_)";
Gavin>         my $elapsed  = tv_interval($start);
Gavin>         return "<!-- START: $template -->\n$data\n<!-- END: $template
Gavin> ($elapsed seconds) -->";
Gavin>     };
Gavin> }
Gavin> 1;

Ugh.  Eval-string.  You can eliminate that with this:

    foreach my $sub (qw( process include )) {
      my $super = __PACKAGE__->can("SUPER::$sub") or die;
      *$sub = sub {
        my $self     = shift;
        my $template = ref $_[0] ? $_[0]->name : $_[0];
        my $start    = [gettimeofday];
        my $data     = $super->($self, @_);
        my $elapsed  = tv_interval($start);
        return "<!-- START: $template -->\n$data\n<!-- END: $template
    ($elapsed seconds) -->";
      };
    }

It's all a matter of programming. :) Note that this $super->() call
is not a method call, but a real coderef call.  Amazingly enough,
to make it a method call, you can just move a few things around:

  my $data = $self->$super(@_);

But the net is exactly the same, since $super is a coderef.

        -- 
        Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@xxxxxxxxxx.xxx> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

_______________________________________________
templates mailing list
templates@xxxxxxxxxxxxxxxx.xxx
http://lists.template-toolkit.org/mailman/listinfo/templates

(message missing)

Re: [Templates] Performance on my new site is a drag
merlyn (Randal L. Schwartz) 22:32 on 25 Oct 2004

Re: [Templates] Performance on my new site is a drag
Perrin Harkins 18:43 on 25 Oct 2004

Re: [Templates] Performance on my new site is a drag
Perrin Harkins 19:18 on 25 Oct 2004

Re: [Templates] Performance on my new site is a drag
Perrin Harkins 20:46 on 25 Oct 2004

Re: [Templates] Performance on my new site is a drag
Geoffrey Young 02:18 on 26 Oct 2004

Re: [Templates] Performance on my new site is a drag
Perrin Harkins 14:40 on 29 Oct 2004

Re: [Templates] Performance on my new site is a drag
Perrin Harkins 15:07 on 29 Oct 2004

Generated at 08:55 on 15 Mar 2005 by mariachi v0.52