Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Accessors in xs

by fireartist (Chaplain)
on Oct 25, 2005 at 09:37 UTC ( [id://502651]=note: print w/replies, xml ) Need Help??


in reply to Accessors in xs

Testing object creation and 2 method-calls, gives a 25% speedup

use strict; use warnings; use Benchmark 'cmpthese'; use lib ('blib/lib', 'blib/arch'); use CMethods; use PerlMethods; my($x, $y); cmpthese( 200_000, { cMethods => sub { my $c = CMethods->new; $x = $c->x; $y = $c->y; }, PerlMethods => sub { my $c = PerlMethods->new; $x = $c->x; $y = $c->y; }, });
Rate PerlMethods cMethods PerlMethods 111297/s -- -17% cMethods 134771/s 21% -- Rate PerlMethods cMethods PerlMethods 111297/s -- -20% cMethods 139179/s 25% -- Rate PerlMethods cMethods PerlMethods 108460/s -- -22% cMethods 139179/s 28% --

Testing object creation and 200 method-calls (to lessen the overhead of creating the object), gives an impresive 78% speedup

use strict; use warnings; use Benchmark 'cmpthese'; use lib ('blib/lib', 'blib/arch'); use CMethods; use PerlMethods; my($x, $y); cmpthese( 50_000, { cMethods => sub { my $c = CMethods->new; for (1..100) { $x = $c->x; $y = $c->y; } }, PerlMethods => sub { my $c = PerlMethods->new; for (1..100) { $x = $c->x; $y = $c->y; } }, });
Rate PerlMethods cMethods PerlMethods 2933/s -- -44% cMethods 5220/s 78% -- Rate PerlMethods cMethods PerlMethods 2958/s -- -44% cMethods 5255/s 78% -- Rate PerlMethods cMethods PerlMethods 2949/s -- -44% cMethods 5246/s 78% --

Here's the code I used:

PerlMethods.pm
package PerlMethods; use strict; sub new { my ($pckg, $x, $y) = @_; my $self = { x => $x, y => $y }; return bless $self, $pckg; } sub x { my ($self) = @_; return $self->{x}; } sub y { my ($self) = @_; return $self->{y}; } 1;
CMethods.pm
package CMethods; use strict; our @ISA = qw(); our $VERSION = '0.01'; require XSLoader; XSLoader::load('CMethods', $VERSION); sub new { my ($pckg, $x, $y) = @_; my $self = { x => $x, y => $y }; return bless $self, $pckg; } 1;
CMethods.xs
#include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "ppport.h" MODULE = CMethods PACKAGE = CMethods PROTOTYPES: DISABLE void x(self) HV *self PREINIT: SV **x; PPCODE: x = hv_fetch( self, "x", 1, 0 ); EXTEND(SP, 1); PUSHs(x ? *x : &PL_sv_undef); void y(self) HV *self PREINIT: SV **y; PPCODE: y = hv_fetch( self, "y", 1, 0 ); EXTEND(SP, 1); PUSHs(y ? *y : &PL_sv_undef);

Replies are listed 'Best First'.
Re^2: Accessors in xs
by robin (Chaplain) on Oct 25, 2005 at 11:54 UTC

    Nice. If I add some other possibilities:

    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:

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

      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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://502651]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-04-23 07:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found