Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: Accessors in xs

by robin (Chaplain)
on Oct 25, 2005 at 11:54 UTC ( #502681=note: print w/ replies, xml ) Need Help??


in reply to Re: Accessors in xs
in thread Accessors in xs

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.


Comment on Re^2: Accessors in xs
Select or Download Code
Re^3: Accessors in xs
by BrowserUk (Pope) on Oct 25, 2005 at 12:24 UTC

    Nice++ It would be interesting to combine your benchmark and Re: Making an Object faster?.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://502681]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (10)
As of 2015-07-04 08:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (58 votes), past polls