Nice. If I add some other possibilities:

PerlSubCall => sub { my $c = PerlMethods->new; for (1..100) { $x = PerlMethods::x($c); $y = PerlMethods::y($c); } }, cSubCall => sub { my $c = CMethods->new; for (1..100) { $x = CMethods::x($c); $y = CMethods::y($c); } }, Direct => sub { my $c = PerlMethods->new; for (1..100) { $x = $c->{x}; $y = $c->{y}; }, Fields => sub { my UsingFields $c = UsingFields->new; for (1..100) { $x = $c->{x}; $y = $c->{y}; } }

I get these results:

Rate PerlMethods PerlSubCall cMethods cSubCall Direct + Fields PerlMethods 1875/s -- -4% -64% -72% -80% + -80% PerlSubCall 1951/s 4% -- -62% -70% -79% + -80% cMethods 5160/s 175% 164% -- -22% -44% + -46% cSubCall 6596/s 252% 238% 28% -- -29% + -31% Direct 9242/s 393% 374% 79% 40% -- + -4% Fields 9597/s 412% 392% 86% 45% 4% + --
which (unsurprisingly) shows that, if you need a massive speedup, there's no alternative to accessing the members directly. For a final iota of speed you can use the fields mechanism: the UsingFields.pm module is:
package UsingFields; use strict; use fields qw(x y); sub new { my ($pckg, $x, $y) = @_; my $self = fields::new($pckg); $self->{x} = $x; $self->{y} = $y; return bless $self, $pckg; } sub x { my ($self) = @_; return $self->{x}; } sub y { my ($self) = @_; return $self->{y}; } 1;

Update: Be warned that fields no longer gives any speedup with the development version of perl, and won't in 5.10 either.


In reply to Re^2: Accessors in xs by robin
in thread Accessors in xs by hardburn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.