Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • I was always under the impression that perl method dispatch was what caused things to take such a long time. If that's the case, then surely having methods execute faster once they've been dispatched to, wouldn't help much.

    Or am I missing the point (or should that be the profile?) here?

    -Dom

    • Perl method dispatch is slow but perl code for accessors is not fast also. Here an example (one accessor is implemented in Perl and another in C with help of Inline::C):

      use Benchmark;

      my $obj = A->new;
      $obj->field1('test');
      $obj->field2('test');

      timethese(1000000,
      &nb sp;         { pure_perl => sub { $obj->field1($obj->field1) },
                  inline_c  => sub { $obj->field2($obj->field2) }
                });

      package A;

      sub new { bless {}, shift }

      sub field1 {
          my $self = shift;

          if(@_) {
              $self->{field1} = $_[0];
          }

          return $self->{field1};
      }

      use Inline C => <<'END';

      SV* field2 (SV* self, ...) {
          SV** temp;
          Inline_Stack_Vars;

          if(!(SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV))
              croak("Not a hash reference");

          if(Inline_Stack_Items > 1) {
              SV* value = Inline_Stack_Item(1);

              if(! hv_store((HV*) SvRV(self), "field2", 6, SvREFCNT_inc(value), 0)) {
                  SvREFCNT_dec(value);
                  croak("Cannot store in hash");
              }
          }

          if(temp = hv_fetch((HV*) SvRV(self), "field2", 6, 0)) {
              return SvREFCNT_inc(*temp);
          } else {
              return &PL_sv_undef;
          }
      }

      END

      Results on my computer:

      Benchmark: timing 1000000 iterations of inline_c, pure_perl...
        inline_c:  2 wallclock secs ( 2.25 usr +  0.00 sys =  2.25 CPU) @ 444444.44/s
      (n=1000000)
      pure_perl:  6 wallclock secs ( 6.67 usr +  0.05 sys =  6.72 CPU) @ 148809.52/s
      (n=1000000)
      --

      Ilya Martynov (http://martynov.org/ [martynov.org])